Example usage for java.util Calendar set

List of usage examples for java.util Calendar set

Introduction

In this page you can find the example usage for java.util Calendar set.

Prototype

public void set(int field, int value) 

Source Link

Document

Sets the given calendar field to the given value.

Usage

From source file:Main.java

public static boolean isInPast(int day, int month, int year) {
    Calendar c = Calendar.getInstance();

    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month - 1);
    c.set(Calendar.DAY_OF_MONTH, day);

    Date pastDate = c.getTime();/*  w  w w. j ava 2s .c  o  m*/

    return (pastDate.getTime() < new Date().getTime());
}

From source file:Main.java

/**
 * ????date???n???????//w ww  .  ja v  a2 s.  c om
 */
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 Calendar buildCalendarInstance(final int year, final int month, final int dayOfMonth) {
    final Calendar c = Calendar.getInstance();
    c.clear();//from w  w  w . ja  va2s.co  m

    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, dayOfMonth);

    return c;
}

From source file:Main.java

public static Calendar getZeroSecondCalendar() {
    Calendar calendar = getCalendar();
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar;
}

From source file:Main.java

public static Date firstDayOfMonth(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);// www .j  av  a 2 s.  co m
    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 Date lastDayOfMonth(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);/*w  w w  .ja va  2  s  .  co m*/
    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 long setTimeToMidnightAndGet(Calendar cal, long epoch) {
    cal.setTime(new Date(epoch));
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);//  w ww  .  j a  v a2  s .  c o m
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTimeInMillis();
}

From source file:Main.java

public static int getDayOfWeek(int year, int week) {
    Calendar calendar = Calendar.getInstance();
    calendar.clear();//from w  ww  .j  a va2  s  . com
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.WEEK_OF_YEAR, week);
    return calendar.get(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

public static int getMonth(int year, int week) {
    Calendar calendar = Calendar.getInstance();
    calendar.clear();/*from  w ww .  ja  v  a2 s .com*/
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.WEEK_OF_YEAR, week);
    return calendar.get(Calendar.MONTH) + 1;
}

From source file:Main.java

public static int getWeekFromDay(int year, int month, int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.clear();//ww  w.  j a  v a2 s. co m
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month - 1);
    calendar.set(Calendar.DAY_OF_MONTH, day);
    return calendar.get(Calendar.WEEK_OF_YEAR);
}