Example usage for java.util Calendar setTime

List of usage examples for java.util Calendar setTime

Introduction

In this page you can find the example usage for java.util Calendar setTime.

Prototype

public final void setTime(Date date) 

Source Link

Document

Sets this Calendar's time with the given Date.

Usage

From source file:net.ceos.project.poi.annotated.bean.MultiTypeObjectBuilder.java

/**
 * Validate the MultiTypeObject based on the object build with the method
 * 'buildMultiTypeObject'/*from  w w w.  j  a v  a  2s  .c o  m*/
 * 
 * @param toValidate
 *            the object to validate
 */
public static void validateMultiTypeObject(MultiTypeObject toValidate) {
    MultiTypeObject base = buildMultiTypeObject();

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());

    Calendar calendarUnmarshal = Calendar.getInstance();
    calendarUnmarshal.setTime(toValidate.getDateAttribute());
    assertEquals(calendar.get(Calendar.YEAR), calendarUnmarshal.get(Calendar.YEAR));
    assertEquals(calendar.get(Calendar.MONTH), calendarUnmarshal.get(Calendar.MONTH));
    assertEquals(calendar.get(Calendar.DAY_OF_MONTH), calendarUnmarshal.get(Calendar.DAY_OF_MONTH));
    LocalDate localDate = LocalDate.now();
    assertEquals(localDate.getDayOfMonth(), toValidate.getLocalDateAttribute().getDayOfMonth());
    assertEquals(localDate.getMonth(), toValidate.getLocalDateAttribute().getMonth());
    assertEquals(localDate.getYear(), toValidate.getLocalDateAttribute().getYear());
    LocalDateTime localDateTime = LocalDateTime.now();
    assertEquals(localDateTime.getDayOfMonth(), toValidate.getLocalDateTimeAttribute().getDayOfMonth());
    assertEquals(localDateTime.getMonth(), toValidate.getLocalDateTimeAttribute().getMonth());
    assertEquals(localDateTime.getYear(), toValidate.getLocalDateTimeAttribute().getYear());
    assertEquals(localDateTime.getHour(), toValidate.getLocalDateTimeAttribute().getHour());

    /* it is possible to have an error below due the time of execution of the test */
    assertEquals(localDateTime.getMinute(), toValidate.getLocalDateTimeAttribute().getMinute());

    assertEquals(base.getStringAttribute(), toValidate.getStringAttribute());
    assertEquals(base.getIntegerAttribute(), toValidate.getIntegerAttribute());
    assertEquals(base.getDoubleAttribute(), toValidate.getDoubleAttribute());
    assertEquals(base.getLongAttribute(), toValidate.getLongAttribute());
    assertEquals(base.getBooleanAttribute(), toValidate.getBooleanAttribute());
    assertEquals(base.getJob().getJobCode(), toValidate.getJob().getJobCode());
    assertEquals(base.getJob().getJobFamily(), toValidate.getJob().getJobFamily());
    assertEquals(base.getJob().getJobName(), toValidate.getJob().getJobName());
    assertEquals(base.getIntegerPrimitiveAttribute(), toValidate.getIntegerPrimitiveAttribute());
    assertEquals(base.getDoublePrimitiveAttribute(), toValidate.getDoublePrimitiveAttribute());
    assertEquals(base.getLongPrimitiveAttribute(), toValidate.getLongPrimitiveAttribute());
    assertEquals(base.isBooleanPrimitiveAttribute(), toValidate.isBooleanPrimitiveAttribute());
    assertEquals(base.getAddressInfo().getAddress(), toValidate.getAddressInfo().getAddress());
    assertEquals(base.getAddressInfo().getNumber(), toValidate.getAddressInfo().getNumber());
    assertEquals(base.getAddressInfo().getCity(), toValidate.getAddressInfo().getCity());
    assertEquals(base.getAddressInfo().getCityCode(), toValidate.getAddressInfo().getCityCode());
    assertEquals(base.getAddressInfo().getCountry(), toValidate.getAddressInfo().getCountry());
    assertEquals(base.getFloatAttribute(), toValidate.getFloatAttribute());
    assertEquals(base.getFloatPrimitiveAttribute(), toValidate.getFloatPrimitiveAttribute());
    assertEquals(base.getUnitFamily(), toValidate.getUnitFamily());
    assertEquals(base.getBigDecimalAttribute(), toValidate.getBigDecimalAttribute());
    assertEquals(base.getShortAttribute(), toValidate.getShortAttribute());
    assertEquals(base.getShortPrimitiveAttribute(), toValidate.getShortPrimitiveAttribute());
    // TODO add new validation below
}

From source file:Main.java

/**
 * Truncate this date, leaving the field specified as the most
 * significant field.//from   ww w . jav a 2 s  .c  o m
 *
 * For example, if you had the datetime of 28 Mar 2002
 * 13:45:01.231, if you passed with HOUR, it would return 28 Mar
 * 2002 13:00:00.000.  If this was passed with MONTH, it would
 * return 1 Mar 2002 0:00:00.000.s
 * 
 * @param date  the date to work with
 * @param field  the field from <code>Calendar</code>
 *  or <code>SEMI_MONTH</code>
 * @return the rounded date
 * @throws IllegalArgumentException if the date is <code>null</code>
 * @throws ArithmeticException if the year is over 280 million
 */
public static Date truncate(Date date, int field) {
    if (date == null) {
        throw new IllegalArgumentException("The date must not be null");
    }
    Calendar gval = Calendar.getInstance();
    gval.setTime(date);
    modify(gval, field, false);
    return gval.getTime();
}

From source file:Main.java

public static String getAge(String fec_nac) {

    DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
    Calendar dob = Calendar.getInstance();

    Date d = null;/*from  w w w. ja  v  a2 s  .co m*/
    try {
        d = df.parse(fec_nac);

    } catch (Exception e) {
    }

    Calendar today = Calendar.getInstance();
    dob.setTime(d);

    int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);

    if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) {
        age--;
    }
    return String.valueOf(age);
}

From source file:Main.java

public static Calendar toCalendar(String s, String format) {
    Calendar c = Calendar.getInstance();
    SimpleDateFormat simpleDateFormat;
    simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
    s = s.replace("T", " "); // car T est invalide
    try {/*from ww w  .  j  a v a2  s  .com*/
        c.setTime(simpleDateFormat.parse(s));
    } catch (ParseException e) {
        //          TODO Auto-generated catch block
        e.printStackTrace();
    }
    return c;
}

From source file:com.storageroomapp.client.util.StorageRoomUtil.java

/**
 * Converts a StorageRoom time String into a Java Calendar
 * @param str the String time field/*ww  w . j  a v a2 s  .c o m*/
 * @return the Calendar, or null if not a valid time String
 */
static public Calendar storageRoomTimeStringToCalendar(String str) {
    initFormatters();
    Calendar cal = Calendar.getInstance(gmt);
    try {
        Date date = timeFieldFormatter.parse(str);
        cal.setTime(date);
    } catch (Exception e) {
        log.error("Error deserializing a StorageRoom time field", e);
    }
    return cal;
}

From source file:com.storageroomapp.client.util.StorageRoomUtil.java

/**
 * Converts a StorageRoom date String into a Java Calendar
 * @param str the String date field//  w  w w .  j a va 2  s . co  m
 * @return the Calendar, or null if not a valid date String
 */
static public Calendar storageRoomDateStringToCalendar(String str) {
    initFormatters();
    Calendar cal = Calendar.getInstance(gmt);
    try {
        Date date = dateFieldFormatter.parse(str);
        cal.setTime(date);
    } catch (Exception e) {
        log.error("Error deserializing a StorageRoom date field", e);
    }
    return cal;
}

From source file:Main.java

public static String getSpecifiedDayAfter(String specifiedDay) {
    Calendar c = Calendar.getInstance();
    Date date = null;/*from  w  w w.j a  v a 2 s.  co  m*/
    try {
        date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    c.setTime(date);
    int day = c.get(Calendar.DATE);
    c.set(Calendar.DATE, day + 1);
    String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
    return dayAfter;
}

From source file:Main.java

public static String getSpecifiedDayBefore(String specifiedDay) {
    Calendar c = Calendar.getInstance();
    Date date = null;/*from ww w.j a v a2s.c  o m*/
    try {
        date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    c.setTime(date);
    int day = c.get(Calendar.DATE);
    c.set(Calendar.DATE, day - 1);
    String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
    return dayBefore;
}

From source file:Main.java

public static Calendar getDateFromString(String string, String format) {
    Calendar calendar = null;

    SimpleDateFormat form = new SimpleDateFormat(format, Locale.US);
    Date date = null;/*from  www. j  av  a 2 s. c o  m*/

    try {
        date = form.parse(string);

        calendar = Calendar.getInstance();
        calendar.setTime(date);
    } catch (java.text.ParseException e) {
    }

    return calendar;
}