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.sourceforge.fenixedu.util.CalendarUtil.java

public static Integer getNumberOfDaysBetweenDates(Date beginDate, Date endDate) {
    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();

    c1.setTime(beginDate);
    c2.setTime(endDate);// w w w  .  j  a  va  2  s . com

    long timeBetweenInMillis = c2.getTimeInMillis() - c1.getTimeInMillis();
    Integer numberDaysBetweenDates = new Integer(new Long(timeBetweenInMillis / (3600000 * 24)).intValue());

    return numberDaysBetweenDates;
}

From source file:ezbake.services.provenance.graph.Utils.java

public static ezbake.base.thrift.DateTime convertDate2DateTime(final java.util.Date theDate) {
    final Calendar cal = new GregorianCalendar();
    cal.setTime(theDate);

    // get calendar parts
    final int year = cal.get(Calendar.YEAR);
    final int month = cal.get(Calendar.MONTH) + 1;
    final int day = cal.get(Calendar.DAY_OF_MONTH);
    final int hour = cal.get(Calendar.HOUR_OF_DAY);
    final int minute = cal.get(Calendar.MINUTE);
    final int second = cal.get(Calendar.SECOND);
    int offsetMinutes = (cal.getTimeZone().getOffset(cal.getTimeInMillis())) / (1000 * 60);

    // set thrift DateTime propertiesd
    final ezbake.base.thrift.DateTime dt = new ezbake.base.thrift.DateTime();
    // Date//from w  w w . j  a v  a 2  s  .c  o m
    final ezbake.base.thrift.Date date = new ezbake.base.thrift.Date();
    date.setMonth((short) month).setDay((short) day).setYear((short) year);
    dt.setDate(date);

    // Time with TimeZone
    final ezbake.base.thrift.Time t = new ezbake.base.thrift.Time();
    boolean afterUtc = offsetMinutes > 0;
    offsetMinutes = Math.abs(offsetMinutes);
    final ezbake.base.thrift.TimeZone tz = new ezbake.base.thrift.TimeZone((short) (offsetMinutes / 60),
            (short) (offsetMinutes % 60), afterUtc);
    t.setHour((short) hour).setMinute((short) minute).setSecond((short) second).setTz(tz);
    dt.setTime(t);

    return dt;
}

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

/**
 * Validate the ObjectConfigurable based on the object build with the method
 * 'buildObjectConfigurable'/* w w w .  j  a  v  a  2 s. c om*/
 * 
 * @param toValidate
 *            the object to validate
 */
public static void validateObjectConfigurable(ObjectConfigurable toValidate) {
    ObjectConfigurable base = buildObjectConfigurable();

    assertEquals(base.getBooleanAttribute(), toValidate.getBooleanAttribute());
    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));
    assertEquals(base.getDoubleAttribute(), toValidate.getDoubleAttribute());
    assertEquals(base.getIntegerAttribute(), toValidate.getIntegerAttribute());
    assertEquals(base.getLongAttribute(), toValidate.getLongAttribute());
    assertEquals(base.getStringAttribute(), toValidate.getStringAttribute());
    Calendar calendar1 = Calendar.getInstance();
    calendar1.setTime(new Date());
    Calendar calendarUnmarshal1 = Calendar.getInstance();
    calendarUnmarshal1.setTime(toValidate.getDateAttribute1());
    assertEquals(calendar1.get(Calendar.YEAR), calendarUnmarshal1.get(Calendar.YEAR));
    assertEquals(calendar1.get(Calendar.MONTH), calendarUnmarshal1.get(Calendar.MONTH));
    assertEquals(calendar1.get(Calendar.DAY_OF_MONTH), calendarUnmarshal1.get(Calendar.DAY_OF_MONTH));
    assertEquals(base.getJob().getJobCode(), toValidate.getJob().getJobCode());
    assertEquals(base.getJob().getJobFamily(), toValidate.getJob().getJobFamily());
    assertEquals(base.getJob().getJobName(), toValidate.getJob().getJobName());
    // TODO add new validation below
}

From source file:model.manager.FileManager.java

public static boolean saveTemplate(ReportObject obj) {

    Date now = new Date();
    Calendar C = Calendar.getInstance();
    C.setTime(now);

    String fileName = C.getTimeInMillis() + ".xml";
    return saveTemplate(obj, fileName);
}

From source file:de.codesourcery.eve.skills.util.Misc.java

public static Calendar stripToDay(Date d) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);/*  w  ww . j a v a 2  s  .co m*/
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal;
}

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

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

    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));

    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());

    // TODO add new validation below
}

From source file:gob.dp.simco.comun.FunctionUtil.java

public static Calendar ponerAnioMesActual(Date fecha, Date fechaActual) {
    try {/*w  w w.  j  a  v a2  s  . c om*/
        Calendar cActual = Calendar.getInstance();
        cActual.setTime(fechaActual);
        Calendar cfecha = Calendar.getInstance();
        cfecha.setTime(fecha);
        //la fecha nueva
        Calendar c1 = Calendar.getInstance();
        c1.set(cActual.get(Calendar.YEAR), cActual.get(Calendar.MONTH), cfecha.get(Calendar.DATE));
        return c1;
    } catch (Exception e) {
        return null;
    }
}

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

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

    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));

    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());

    // TODO add new validation below
}

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

/**
 * Validate the ObjectNull based on the object build with the method
 * 'buildObjectNull'//from  w w w .ja  v a 2s  . com
 * 
 * @param toValidate
 *            the object to validate
 */
public static void validateObjectNull(ObjectNull toValidate) {
    ObjectNull base = buildObjectNull();

    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));
    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

public static String getStringByFormat(String strDate, String format) {
    String mDateTime = null;/*from  w  w w .  ja  v a2s .c  o m*/
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(dateFormatYMDHMS);
        c.setTime(mSimpleDateFormat.parse(strDate));
        SimpleDateFormat mSimpleDateFormat2 = new SimpleDateFormat(format);
        mDateTime = mSimpleDateFormat2.format(c.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mDateTime;
}