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:Utils.java

public static Date getEndOfDay(Date day, Calendar cal) {
    if (day == null)
        day = new Date();
    cal.setTime(day);/*from  w  w  w. j  a  v  a 2  s. c  om*/
    cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY));
    cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
    cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));
    return cal.getTime();
}

From source file:Main.java

/**
 * Takes a date value as used in CPLC Date fields (represented by 2 bytes)
 * //from   w  w  w  .jav a2s. c o m
 * @param paramByte1
 * @param paramByte2
 * @throws IllegalArgumentException
 * @return
 */
public static Date calculateCplcDate(byte[] dateBytes) throws IllegalArgumentException {
    if (dateBytes == null || dateBytes.length != 2) {
        throw new IllegalArgumentException("Error! CLCP Date values consist always of exactly 2 bytes");
    }
    // current time
    Calendar now = Calendar.getInstance();

    int year = now.get(Calendar.YEAR);
    int startYearOfCurrentDecade = year - (year % 10);

    int days = 100 * (dateBytes[0] & 0xF) + 10 * (0xF & dateBytes[1] >>> 4) + (dateBytes[1] & 0xF);

    if (days > 366) {
        throw new IllegalArgumentException("Invalid date (or are we parsing it wrong??)");
    }

    Calendar calculatedDate = Calendar.getInstance();
    calculatedDate.clear();
    calculatedDate.set(Calendar.YEAR, startYearOfCurrentDecade + (0xF & dateBytes[0] >>> 4));
    calculatedDate.set(Calendar.DAY_OF_YEAR, days);
    while (calculatedDate.after(now)) {
        calculatedDate.add(Calendar.YEAR, -10);
    }
    return calculatedDate.getTime();
}

From source file:Main.java

/**
 * Creates a calendar object with the next absolute time determined by hour and minute
 * @param hourOfDay/*w w  w.  j  av a  2s . c o  m*/
 * @param minute
 * @return
 */
public static Calendar getNextAlarmTimeAbsolute(int hourOfDay, int minute) {
    Calendar cal = Calendar.getInstance();
    Calendar calNow = (Calendar) cal.clone();

    cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
    cal.set(Calendar.MINUTE, minute);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    if (cal.compareTo(calNow) <= 0) {
        //Today's time passed, count to tomorrow
        cal.add(Calendar.DATE, 1);
    }
    return cal;
}

From source file:Main.java

private static final Calendar getUtcDate(int year, int month, int dayOfMonth) {
    final Calendar calendar = Calendar.getInstance(UTC_TIMEZONE, Locale.US);
    calendar.clear();/*from   w  w w  . j  a v a2  s  .  co m*/
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    return calendar;
}

From source file:com.liusoft.dlog4j.util.DateUtils.java

/**
 * ??//from   w  w  w.ja v  a2  s.c om
 * @param year
 * @param month
 * @param date
 * @return
 */
public static Calendar getDateBegin(int year, int month, int date) {
    Calendar begin_time = Calendar.getInstance();
    begin_time.set(Calendar.YEAR, year);
    begin_time.set(Calendar.MONTH, month - 1);
    begin_time.set(Calendar.DATE, date);
    begin_time.set(Calendar.HOUR_OF_DAY, 0);
    begin_time.set(Calendar.MINUTE, 0);
    begin_time.set(Calendar.SECOND, 0);
    begin_time.set(Calendar.MILLISECOND, 0);
    return begin_time;
}

From source file:Main.java

public static Date firstTimeOfMonth(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(date.getTime());

    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.DAY_OF_MONTH, 1);

    Date dateBegin = new Date();
    dateBegin.setTime(calendar.getTimeInMillis());
    return dateBegin;
}

From source file:Main.java

public static int getStudyDay(Context context) {
    String startStr = getSP(context, SP_LOGIN).getString(SP_KEY_LOGIN_STUDY_STARTTIME, "");
    if (!startStr.equals("")) {
        long start = Long.parseLong(startStr);
        long current = Calendar.getInstance().getTimeInMillis();

        Calendar s = Calendar.getInstance();
        s.setTimeInMillis(start);/*from w ww. j  ava 2s  .c om*/
        s.set(Calendar.HOUR_OF_DAY, 3);
        s.set(Calendar.MINUTE, 0);
        s.set(Calendar.SECOND, 0);
        s.set(Calendar.MILLISECOND, 0);

        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(current);
        //         c.set(Calendar.HOUR_OF_DAY, 12);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        start = s.getTimeInMillis();
        current = c.getTimeInMillis();

        return (int) ((current - start) / (24 * 60 * 60 * 1000));
    } else {
        return -1;
    }
}

From source file:Main.java

public static boolean isDateAfterToday(Calendar calendar) {
    Calendar validationCalendar = Calendar.getInstance();
    validationCalendar.setTime(calendar.getTime());
    validationCalendar.set(Calendar.MILLISECOND, todayCalendar.get(Calendar.MILLISECOND));
    validationCalendar.set(Calendar.SECOND, todayCalendar.get(Calendar.SECOND));
    validationCalendar.set(Calendar.MINUTE, todayCalendar.get(Calendar.MINUTE));
    validationCalendar.set(Calendar.HOUR_OF_DAY, todayCalendar.get(Calendar.HOUR_OF_DAY));
    return validationCalendar.getTime().after(todayCalendar.getTime());
}

From source file:Main.java

public static boolean isDateBeforeToday(Calendar calendar) {
    Calendar validationCalendar = Calendar.getInstance();
    validationCalendar.setTime(calendar.getTime());
    validationCalendar.set(Calendar.MILLISECOND, todayCalendar.get(Calendar.MILLISECOND));
    validationCalendar.set(Calendar.SECOND, todayCalendar.get(Calendar.SECOND));
    validationCalendar.set(Calendar.MINUTE, todayCalendar.get(Calendar.MINUTE));
    validationCalendar.set(Calendar.HOUR_OF_DAY, todayCalendar.get(Calendar.HOUR_OF_DAY));
    return validationCalendar.getTime().before(todayCalendar.getTime());
}

From source file:com.joken.common.utils.ConverterUtils.java

/**
 * ?//  ww w.  j  a  v a  2 s  . c  o  m
 * 
 * @param time
 *            :08:30:00
 * @param separator
 *            :
 * @return 
 */
public static Long string2TimeSeconds(String time, String separator) {
    Calendar cl = Calendar.getInstance();
    String[] times = time.split(separator);
    cl.set(Calendar.HOUR_OF_DAY, Integer.valueOf(times[0]));
    cl.set(Calendar.MINUTE, Integer.valueOf(times[1]));
    cl.set(Calendar.SECOND, Integer.valueOf(times[2]));
    cl.set(Calendar.MILLISECOND, 0);
    return cl.getTimeInMillis() / 1000;
}