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 boolean isSameDay(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);
    cal2.setTime(date2);/*from  www.j a  v  a 2  s.com*/
    return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
}

From source file:Main.java

public static int getWeekOfYear(Date date) {
    Calendar c = Calendar.getInstance();
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.setTime(date);
    int week = c.get(Calendar.WEEK_OF_YEAR) - 1;
    week = week == 0 ? 52 : week;//  w w w.j a v  a 2 s .com
    return week > 0 ? week : 1;
}

From source file:Main.java

public static String formatDate(String date, int minutes) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date d = null;/*w  w  w.  j  a  v a  2s  . c o  m*/
    try {
        d = sdf.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(d);
    calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - minutes);
    return sdf.format(calendar.getTime());
}

From source file:Main.java

public static boolean isSameDay(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);
    cal2.setTime(date2);/* w  w w . j  a va2  s  .  c om*/
    boolean sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
    return sameDay;
}

From source file:Main.java

/**
 * Returns milliseconds of the date argument dt. If the argument
 * isIncludeTime is false then the returned milliseconds does not include
 * time./* ww w  . j  a  v  a  2  s.  c om*/
 * 
 * @param dt
 * @param isIncludeTime
 * @return
 */
private static long getDate(Date dt, boolean isIncludeTime) {
    Calendar cal = GregorianCalendar.getInstance();
    cal.setTime(dt);
    if (!isIncludeTime) {
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
    }
    return cal.getTimeInMillis();
}

From source file:Main.java

public static boolean sameDate(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);
    cal2.setTime(date2);/*  w w  w . j a va  2 s  .co  m*/
    boolean sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
    return sameDay;
}

From source file:ConversionUtil.java

/**
 * convert into java.sql.Time (or into java.util.Calendar
 * /*  w w w  .ja  v  a  2  s.c  o  m*/
 * @param date
 *          The date containing the time.
 * @param am
 *          Whether this should be am (true) or pm (false)
 * @return
 */
public static Time convertDateToTime(Date date, boolean am) {
    if (date == null) {
        return null;
    }

    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);

    if (am) {
        // Check to make sure that the hours are indeed am hours
        if (hourOfDay > 11) {
            cal.set(Calendar.HOUR_OF_DAY, hourOfDay - 12);
            date.setTime(cal.getTimeInMillis());
        }
    } else {
        // Check to make sure that the hours are indeed pm hours
        if (cal.get(Calendar.HOUR_OF_DAY) < 11) {
            cal.set(Calendar.HOUR_OF_DAY, hourOfDay + 12);
            date.setTime(cal.getTimeInMillis());
        }
    }
    return new Time(date.getTime());
}

From source file:Main.java

private static boolean sameDay(Date a, Date b) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(a);
    cal2.setTime(b);/*from   w  w  w . j a v  a 2s .c  o m*/
    return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
}

From source file:edu.zipcloud.core.util.DateUtil.java

public static Date getEndOfDay(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    calendar.set(Calendar.MILLISECOND, 999);
    return calendar.getTime();
}

From source file:edu.zipcloud.core.util.DateUtil.java

public static Date getStartOfDay(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar.getTime();
}