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 String getFirstDayOfLastMonth() {
    Calendar c = Calendar.getInstance();
    int month = c.get(Calendar.MONTH);
    c.set(Calendar.MONTH, month - 1);
    c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    return df.format(c.getTime());
}

From source file:Main.java

public static Date getFirstDayOfMonth(Date dateTime) {
    if (dateTime == null)
        return dateTime;
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(dateTime);/*from w ww  .j a v  a 2  s  . c  om*/
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    return calendar.getTime();
}

From source file:Main.java

public static void clearTimeInfo(Calendar theDate) {
    if (theDate == null) {
        return;//from w  ww .  j a  va 2  s  . c o m
    }

    theDate.set(Calendar.HOUR_OF_DAY, 0);
    theDate.set(Calendar.MINUTE, 0);
    theDate.set(Calendar.SECOND, 0);
}

From source file:Main.java

public static Calendar initCalendar(Date date) {

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);/* w w w  .  ja  va 2 s.  co  m*/
    calendar.set(Calendar.HOUR, 10);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar;
}

From source file:Main.java

public static Date getDateStart(Date date) {
    Calendar calendar = Calendar.getInstance();

    calendar.setTime(date);/*from w w w.j ava2s. c o  m*/
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    return calendar.getTime();
}

From source file:Main.java

public static Date getDateEnd(Date date) {
    Calendar calendar = Calendar.getInstance();

    calendar.setTime(date);/*from   w  ww. j  ava2s .c  om*/
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);

    return calendar.getTime();
}

From source file:Main.java

private static Date getDateBefore() {
    Date nowtime = new Date();
    Calendar now = Calendar.getInstance();
    now.setTime(nowtime);//from   w w w  . j a  v a2s .  co  m
    now.set(Calendar.DATE, now.get(Calendar.DATE) - LOG_SAVE_DAYS);
    return now.getTime();
}

From source file:Main.java

/**
 * Decode a 16-bit encoded DOS date/time into a java date/time.
 * /* w  ww.ja va  2  s.c  o  m*/
 * @param dosDate
 * @param dosTime
 * @return long
 */
public static long decodeDateTime(int dosDate, int dosTime) {
    final Calendar cal = Calendar.getInstance();

    cal.set(Calendar.MILLISECOND, 0);
    cal.set(Calendar.SECOND, (dosTime & 0x1f) * 2);
    cal.set(Calendar.MINUTE, (dosTime >> 5) & 0x3f);
    cal.set(Calendar.HOUR_OF_DAY, dosTime >> 11);

    cal.set(Calendar.DATE, dosDate & 0x1f);
    cal.set(Calendar.MONTH, ((dosDate >> 5) & 0x0f) - 1);
    cal.set(Calendar.YEAR, 1980 + (dosDate >> 9));

    return cal.getTimeInMillis();
}

From source file:Main.java

public static Date hourRoundPlusHour(Date date, int n) {
    Calendar calendar = Calendar.getInstance();

    calendar.setTime(date);//from   w  w w.  j  ava2 s. co m
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 0);

    calendar.add(Calendar.HOUR_OF_DAY, n);

    return calendar.getTime();
}

From source file:Main.java

public static Calendar getCalendarForTime(int repeatMode, int dayOfWeek, int hour, int minute, int second) {
    Calendar calendarNow = Calendar.getInstance();
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, second);
    calendar.set(Calendar.MILLISECOND, 0);
    if (repeatMode == 2) {
        int dayOfWeekAndroid = 0; // 7 stands for sunday for interface, but for android, sunday stands for 1.
        dayOfWeekAndroid = dayOfWeek % 7 + 1;
        calendar.set(Calendar.DAY_OF_WEEK, dayOfWeekAndroid);
    }//www. j a va 2 s.  co m

    // make sure the desire alarm time is in future.
    int tryCount = 0;
    int tryCountMax = 62;
    while (calendar.getTimeInMillis() < calendarNow.getTimeInMillis() && tryCount < tryCountMax) {
        if (repeatMode == 1) {
            calendar.add(Calendar.DAY_OF_YEAR, 1);
        } else if (repeatMode == 2) {
            calendar.add(Calendar.DAY_OF_YEAR, 7);
        }
        tryCount++;
    }
    Log.v("cpeng", "getCalendearForTime target info: " + calendar.toString());
    return calendar;
}