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 Date getStartOfMonth(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);// www  .java 2  s . c o m
    calendar.set(Calendar.DATE, 1);
    return calendar.getTime();
}

From source file:Main.java

public static Calendar setAsToday(Calendar aoCalendar) {
    Calendar loCalendar = Calendar.getInstance();
    aoCalendar.set(Calendar.YEAR, loCalendar.get(Calendar.YEAR));
    aoCalendar.set(Calendar.MONTH, loCalendar.get(Calendar.MONTH));
    aoCalendar.set(Calendar.DAY_OF_MONTH, loCalendar.get(Calendar.DAY_OF_MONTH));
    return aoCalendar;
}

From source file:Main.java

public static Calendar calculateNextAlarmTime(int repetition) {
    if (repetition == 0)
        return null;
    Calendar alarmTime = Calendar.getInstance();
    alarmTime.set(Calendar.HOUR_OF_DAY, 20);
    alarmTime.set(Calendar.MINUTE, 0);
    alarmTime.set(Calendar.SECOND, 0);

    int day_of_week = alarmTime.get(Calendar.DAY_OF_WEEK) - 2;
    if (day_of_week == -1)
        day_of_week = 6;// sunday
    repetition &= 0x7f;/*from  w  w  w  . j  av a  2s  .  c  o m*/
    int rot = (repetition >> (day_of_week + 1)) | (repetition << (7 - day_of_week - 1));
    rot &= 0x7f;
    int ndays = 0;
    for (ndays = 0; ndays < 7; ndays++) {
        if ((rot & (1 << ndays)) != 0)
            break;
    }
    alarmTime.add(Calendar.DATE, ndays + 1);

    return alarmTime;
}

From source file:Main.java

public static Date trimSeconds(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);// w ww  .  j a va  2 s  .  co  m
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.set(Calendar.SECOND, 0);
    return calendar.getTime();
}

From source file:Main.java

public static long getTodayZeroTimeInMillis() {
    Calendar calendar = getZeroSecondCalendar();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    return calendar.getTimeInMillis();
}

From source file:Main.java

public static Calendar getEndOfDate(long time) {
    Calendar ret = Calendar.getInstance();
    ret.setTimeInMillis(time);//w  w  w .  j  a  v  a  2  s .c om
    ret.set(Calendar.HOUR_OF_DAY, 23);
    ret.set(Calendar.MINUTE, 59);
    ret.set(Calendar.SECOND, 59);
    ret.set(Calendar.MILLISECOND, 999);

    return ret;
}

From source file:Main.java

public static Calendar getBeginOfDate(long time) {
    Calendar ret = Calendar.getInstance();
    ret.setTimeInMillis(time);/*from w w  w . j ava  2s  .  c  o m*/
    ret.set(Calendar.HOUR_OF_DAY, 0);
    ret.set(Calendar.MINUTE, 0);
    ret.set(Calendar.SECOND, 0);
    ret.set(Calendar.MILLISECOND, 0);

    return ret;
}

From source file:Main.java

public static long getStartOfTheDay(long date) {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(date);/*from ww  w  . j  a  va 2  s.  c o  m*/

    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);

    return c.getTimeInMillis();
}

From source file:Main.java

public static Date getEndOfDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);/*  ww  w  .j a v  a  2s .com*/
    calendar.set(Calendar.HOUR, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    return calendar.getTime();
}

From source file:Main.java

/**
 * Get the SunSet for the given date//from  ww  w . ja  va 2s. c o m
 * @param date - input date
 * @return - Date with time specifying the Sun Set
 */
public static Date getSunSet(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, 18);
    cal.set(Calendar.MINUTE, 00);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    Date ret = cal.getTime();
    return ret;
}