Example usage for java.util Calendar HOUR_OF_DAY

List of usage examples for java.util Calendar HOUR_OF_DAY

Introduction

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

Prototype

int HOUR_OF_DAY

To view the source code for java.util Calendar HOUR_OF_DAY.

Click Source Link

Document

Field number for get and set indicating the hour of the day.

Usage

From source file:Main.java

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

    calendar.setTime(date);//from   ww  w  .ja  v a  2  s  .c  o 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 final String getStandardTime(int minute, int hour) {
    SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.CHINA);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    return format.format(calendar.getTime());
}

From source file:Main.java

public static String getCurrentTimeString(int s) {
    //s=0 for star time s=1 for end time
    Calendar currentCalendar = Calendar.getInstance();
    int hours = currentCalendar.get(Calendar.HOUR_OF_DAY);
    int minutes = currentCalendar.get(Calendar.MINUTE);

    if (minutes < 30) {
        currentCalendar.setTimeInMillis(currentCalendar.getTimeInMillis() + (30 - minutes) * 60 * 1000);
    }// w  w  w. java  2s  .  c  o m
    if (minutes > 30) {
        currentCalendar.setTimeInMillis(currentCalendar.getTimeInMillis() + (60 - minutes) * 60 * 1000);
    }
    if (s == 1)
        currentCalendar.setTimeInMillis(currentCalendar.getTimeInMillis() + 30 * 60 * 1000);

    return timeFormat.format(currentCalendar.getTimeInMillis());
}

From source file:Main.java

public static String toYYYYMMDDHHMMSS(Calendar source) {
    if (source == null) {
        return "Can't toYYYYMMDDHHMMSS source null";
    }//from  w w w . j  a  v a2s.  c o m
    int YYYY = source.get(Calendar.YEAR);
    int MM = source.get(Calendar.MONTH);
    int DD = source.get(Calendar.DAY_OF_MONTH);
    int HH = source.get(Calendar.HOUR_OF_DAY);
    int mm = source.get(Calendar.MINUTE);
    int SS = source.get(Calendar.SECOND);

    return YYYY + "-" + MM + "-" + DD + " " + HH + ":" + mm + ":" + SS;
}

From source file:Util.java

public static String formatAsMySQLDatetime(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);//from w  ww.  j av  a2  s .  c om

    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH) + 1;
    int day = calendar.get(Calendar.DATE);

    int hours = calendar.get(Calendar.HOUR_OF_DAY);
    int minutes = calendar.get(Calendar.MINUTE);
    int seconds = calendar.get(Calendar.SECOND);

    return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
}

From source file:Main.java

public static Long getYesterday() {
    Calendar yesterday = Calendar.getInstance();
    yesterday.setTimeZone(TimeZone.getDefault());

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

    int day = yesterday.get(Calendar.DAY_OF_YEAR);

    yesterday.set(Calendar.DAY_OF_YEAR, day - 1);

    return yesterday.getTimeInMillis();
}

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);
    }/*from   w w  w  . j a  v a 2s .  c om*/

    // 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;
}

From source file:Main.java

public static long getEndOfMonthMs(int year, int month) {
    final Calendar date = Calendar.getInstance();

    date.set(Calendar.YEAR, year);
    date.set(Calendar.MONTH, month);
    date.set(Calendar.DAY_OF_MONTH, date.getActualMaximum(Calendar.DAY_OF_MONTH));
    date.set(Calendar.HOUR_OF_DAY, date.getActualMaximum(Calendar.HOUR_OF_DAY));
    date.set(Calendar.MINUTE, date.getActualMaximum(Calendar.MINUTE));
    date.set(Calendar.SECOND, date.getActualMaximum(Calendar.SECOND));
    date.set(Calendar.MILLISECOND, date.getActualMaximum(Calendar.MILLISECOND));
    return date.getTimeInMillis();
}

From source file:Main.java

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

From source file:Main.java

/**
 * Get the SunSet for the given date/*  www  .j a v  a  2 s.  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;
}