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 getEndTime() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date dateNow = new Date();
    Calendar cl = Calendar.getInstance();
    cl.setTime(dateNow);
    Date dateFrom = cl.getTime();
    return sdf.format(dateFrom);
}

From source file:Main.java

public static Date dayBegin(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);/* w  ww.ja  v  a 2 s. co  m*/
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    return c.getTime();
}

From source file:Main.java

public static Date getNextSaturday(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
        cal.add(Calendar.DAY_OF_WEEK, 1);
    }//w  w w.  j  a  v a2 s.co  m
    return cal.getTime();
}

From source file:Main.java

public static Date getNextMonday(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
        cal.add(Calendar.DAY_OF_WEEK, 1);
    }/* www  .  ja v  a 2  s .  c  om*/
    return cal.getTime();
}

From source file:Main.java

public static Date addMouth(Date date, int step) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.MONTH, step);
    return calendar.getTime();
}

From source file:Main.java

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

From source file:Main.java

public static int getTimestamp(String targetTime, long currentTime) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar cal = Calendar.getInstance();
    cal.setTime(sdf.parse(targetTime));
    long time1 = cal.getTimeInMillis();
    long between_days = (time1 - currentTime) / (1000 * 3600 * 24);
    return Integer.parseInt(String.valueOf(between_days));
}

From source file:Main.java

public static Date addYear(Date date, int step) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.YEAR, step);
    return calendar.getTime();
}

From source file:Main.java

public static String getYearFromDate(Date date) {

    Calendar c = Calendar.getInstance();
    c.setTime(date);
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
    String s = formatter.format(c.getTime());

    return s;/*from w  w w .j  a v a2s .c om*/
}

From source file:Main.java

public static boolean isSameDate(Date date1, Date date2) {

    Calendar cal = Calendar.getInstance();
    cal.setTime(date1);
    Calendar selectedDate = Calendar.getInstance();
    selectedDate.setTime(date2);/*from  w w  w .j av a 2 s.  co m*/

    return cal.get(Calendar.DAY_OF_YEAR) == selectedDate.get(Calendar.DAY_OF_YEAR);
}