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:Main.java

public static String getStringByOffset(String strDate, String format, int calendarField, int offset) {
    String mDateTime = null;/* w w w  .  j  a  v  a 2 s. c  o m*/
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        c.setTime(mSimpleDateFormat.parse(strDate));
        c.add(calendarField, offset);
        mDateTime = mSimpleDateFormat.format(c.getTime());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return mDateTime;
}

From source file:Main.java

public static String getStringByOffset(Date date, String format, int calendarField, int offset) {
    String strDate = null;/*from ww w  .j  a  va  2 s  .  co m*/
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        c.setTime(date);
        c.add(calendarField, offset);
        strDate = mSimpleDateFormat.format(c.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return strDate;
}

From source file:com.fluke.util.Util.java

public static String getTime(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE);

}

From source file:Main.java

public static String getStringByOffset(String strDate, String format, int calendarField, int offset) {
    String mDateTime = null;//  www  . ja  va2  s  .  c o m
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        c.setTime(mSimpleDateFormat.parse(strDate));
        c.add(calendarField, offset);
        mDateTime = mSimpleDateFormat.format(c.getTime());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return mDateTime;
}

From source file:Main.java

public static String getStringByOffset(Date date, String format, int calendarField, int offset) {
    String strDate = null;//from  ww w.  jav a 2  s. c  om
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        c.setTime(date);
        c.add(calendarField, offset);
        strDate = mSimpleDateFormat.format(c.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return strDate;
}

From source file:Main.java

public static String convertToHumanReadableTime(Date givenDate, long currentTimeLong) {

    Calendar currentTime = Calendar.getInstance();
    currentTime.setTimeZone(TimeZone.getTimeZone("UTC"));
    currentTime.setTimeInMillis(currentTimeLong);

    Calendar givenTime = Calendar.getInstance();
    givenTime.setTimeZone(TimeZone.getTimeZone("UTC"));
    givenTime.setTime(givenDate);

    // Step 1: To see if time difference is less than 24 hours of not
    long timeDiff = currentTime.getTimeInMillis() - givenTime.getTimeInMillis();
    if (timeDiff <= 0) {
        return "Now";
    }/* www .  jav  a  2 s  .  c o m*/

    String humanString = null;
    // Checking if timeDiff is less than 24 or not
    if ((timeDiff / TIME_DAY) >= 1) {
        // days
        int days = (int) (timeDiff / TIME_DAY);
        humanString = String.format(Locale.getDefault(), "%dd", days);
    } else {
        // checking if greater than hour
        if ((timeDiff / TIME_HOUR) >= 1) {
            humanString = String.format(Locale.getDefault(), "%dh", (timeDiff / TIME_HOUR));
        } else if ((timeDiff / TIME_MINUTE) >= 1) {
            humanString = String.format(Locale.getDefault(), "%dm", (timeDiff / TIME_MINUTE));
        } else {
            humanString = String.format(Locale.getDefault(), "%ds", (timeDiff / TIME_MILLIS));
        }
    }

    return humanString;
}

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

/**
 * Validate the CascadeL3 based on the object build with the method
 * 'buildCascadeL3'//from ww w  .j a v a2s . c o  m
 * 
 * @param toValidate
 *            the object to validate
 */
public static void validateCascadeL3(CascadeL3 toValidate) {
    CascadeL3 base = buildCascadeL3();

    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:Main.java

public static Date getDateTimeFrom(Date time, Date date) {
    Date iTime, iDate;//from   w  ww  .jav a 2s  .co  m
    Calendar calendar = Calendar.getInstance();

    if (date == null) {
        iTime = time;
        iDate = new Date();
    } else {
        iTime = time;
        iDate = date;
    }

    Calendar calendarDate = Calendar.getInstance();
    calendarDate.setTime(iDate);

    Calendar calendarTime = Calendar.getInstance();
    if (iTime != null)
        calendarTime.setTime(iTime);

    if (iTime != null) {
        calendar.set(Calendar.MINUTE, calendarTime.get(Calendar.MINUTE));
        calendar.set(Calendar.HOUR_OF_DAY, calendarTime.get(Calendar.HOUR_OF_DAY));
    } else {
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    //calendar.set(Calendar.AM_PM, calendarTime.get(Calendar.AM_PM) );

    calendar.set(Calendar.DAY_OF_MONTH, calendarDate.get(Calendar.DAY_OF_MONTH));
    calendar.set(Calendar.MONTH, calendarDate.get(Calendar.MONTH));
    calendar.set(Calendar.YEAR, calendarDate.get(Calendar.YEAR));

    Date ret = calendar.getTime();
    Log.v("!!!!!!!!!! calendar=", "" + ret);

    return ret;
}

From source file:Main.java

public static String getStringByFormat(String strDate, String format, String outFormat) {
    String mDateTime = null;// w ww  .j a v  a 2s. com
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        c.setTime(mSimpleDateFormat.parse(strDate));
        SimpleDateFormat mSimpleDateFormat2 = new SimpleDateFormat(outFormat);
        mDateTime = mSimpleDateFormat2.format(c.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mDateTime;
}

From source file:com.oncecorp.visa3d.bridge.utility.JdbcUtils.java

/**
 * This method converts the java date into the long that point to the beginning
 * of the day./*from  w  ww.j av  a2s  .  co  m*/
 * @param date The converted date object.
 * @param startEnd <tt>true</tt> day start time, <tt>false</tt> day end time.
 * @return The long that point to the beginning or end of the day.
 */
public static long dayValue(Date date, boolean startEnd) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR, startEnd ? 0 : 23);
    calendar.set(Calendar.MINUTE, startEnd ? 0 : 59);
    calendar.set(Calendar.SECOND, startEnd ? 0 : 59);
    calendar.set(Calendar.MILLISECOND, startEnd ? 0 : 999);
    return calendar.getTime().getTime();
}