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 addDays(Date date, int days) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DAY_OF_MONTH, days);
    return cal.getTime();
}

From source file:Main.java

public static int getMonth(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.MONTH);
}

From source file:Main.java

public static Date addDay(Date date, int n) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DATE, n);/*w ww .  ja  va 2  s .c  o  m*/
    return cal.getTime();

}

From source file:Main.java

public static Date addMonth(Date date, int n) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.MONTH, n);/*w ww . ja  v a  2  s  .com*/
    return cal.getTime();

}

From source file:Main.java

/**
 * ????date???n???????//from w  w w  .j  a  v a2s  . c o m
 */
public static Date getDateAfter(Date date, int n) {
    Calendar now = Calendar.getInstance();
    now.setTime(date);
    now.set(Calendar.DATE, now.get(Calendar.DATE) + n);
    return now.getTime();
}

From source file:Main.java

public static Integer GetDayOfWeekNumber(Date d) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    return cal.get(Calendar.DAY_OF_WEEK);
}

From source file:Main.java

public static Date firstDayOfMonth(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.DAY_OF_MONTH, Calendar.getInstance().getActualMinimum(Calendar.DAY_OF_MONTH));
    cal.set(Calendar.HOUR_OF_DAY, Calendar.getInstance().getActualMinimum(Calendar.HOUR_OF_DAY));
    cal.set(Calendar.MINUTE, Calendar.getInstance().getActualMinimum(Calendar.MINUTE));
    return cal.getTime();
}

From source file:Main.java

public static int dayForWeek(String pTime) throws Exception {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.setTime(format.parse(pTime));
    int dayForWeek = 0;

    return c.get(Calendar.DAY_OF_WEEK);
}

From source file:Main.java

public static Date lastDayOfMonth(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.DAY_OF_MONTH, Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH));
    cal.set(Calendar.HOUR_OF_DAY, Calendar.getInstance().getActualMaximum(Calendar.HOUR_OF_DAY));
    cal.set(Calendar.MINUTE, Calendar.getInstance().getActualMaximum(Calendar.MINUTE));
    return cal.getTime();
}

From source file:Main.java

public static Date getFirstDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.DATE, 1);
    return calendar.getTime();
}