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 Date getPreDay(Date presentDate) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(presentDate);
    calendar.add(Calendar.DAY_OF_MONTH, -1);
    presentDate = calendar.getTime();//  w  ww .  j a  v  a  2s . c  o  m
    return presentDate;
}

From source file:Main.java

/**
 * Get First Date of month/*from ww  w .ja  v a 2s. c  om*/
 */
public static int getDayFromDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.DAY_OF_WEEK);
}

From source file:Main.java

private static Date getDistanceDate(Date date, int day) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DATE, day);/*  w w  w. j a  va 2s  .co m*/
    return cal.getTime();
}

From source file:Main.java

public static Calendar convertTime(int fetchDate) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date((long) fetchDate * 1000));
    return cal;//from  ww  w . j  a va  2  s . co  m
}

From source file:Main.java

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

From source file:Main.java

public static int getNumberOfDaysInPreviousMonth(Date someDayInCurrentMonth) {
    Calendar lastMonthCal = Calendar.getInstance();
    lastMonthCal.setTime(someDayInCurrentMonth);
    lastMonthCal.add(Calendar.MONTH, -1);
    return lastMonthCal.getActualMaximum(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

public static boolean isSameDay(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);

    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);/* w ww .j a va 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 daysBetween(java.sql.Date date1, java.sql.Date date2) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date1);
    long time1 = cal.getTimeInMillis();
    cal.setTime(date2);/*w  w  w  . j av a  2s . co m*/
    long time2 = cal.getTimeInMillis();
    long between_days = (time2 - time1) / (1000 * 3600 * 24);

    return Integer.parseInt(String.valueOf(between_days));
}

From source file:Main.java

public static boolean isSameMonth(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);

    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);/*  ww  w.j a va2 s  .c  o m*/

    return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
}

From source file:Main.java

public static int getDayOfWeek(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    return c.get(Calendar.DAY_OF_WEEK);
}