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

/**
 * Format a time value, including the AM/PM indicator.
 * /*from www  . ja  v a2  s .  com*/
 * @param time the time to format
 * @return the formatted time
 */
public static String formatTime(Calendar time) {
    // TODO: 24 hour clock support
    int hour = time.get(Calendar.HOUR_OF_DAY);
    int minute = time.get(Calendar.MINUTE);
    String ampm = (hour < 12 ? " AM" : " PM");
    if (hour == 0)
        hour = 12;
    else if (hour > 12)
        hour -= 12;
    if (minute < 10)
        return hour + ":0" + minute + ampm;
    else
        return hour + ":" + minute + ampm;
}

From source file:Main.java

/**
 * Packs java time value into an MS-DOS time value.
 * @param time the time value/*from  www . j a va 2 s.co m*/
 * @return the MS-DOS packed time
 */
public static int packTime(long time) {
    Calendar c = Calendar.getInstance();
    c.setTime(new Date(time));

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

    /*
     * Here is how MS-DOS packs a time value:
     * 0-4: seconds (divided by 2 because we only have 5 bits = 32 different numbers)
     * 5-10: minutes (6 bits = 64 possible values)
     * 11-15: hours (5 bits = 32 possible values)
     *
     * source: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724247(v=vs.85).aspx
     */
    return (hours << 11) & (minutes << 5) & (seconds / 2);
}

From source file:Main.java

/**
 * Converts input time from Java to DOS format
 * @param time/*from   ww w .  j  ava  2s.c o  m*/
 * @return time in DOS format 
 */
public static long javaToDosTime(long time) {

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(time);

    int year = cal.get(Calendar.YEAR);
    if (year < 1980) {
        return (1 << 21) | (1 << 16);
    }
    return (year - 1980) << 25 | (cal.get(Calendar.MONTH) + 1) << 21 | cal.get(Calendar.DATE) << 16
            | cal.get(Calendar.HOUR_OF_DAY) << 11 | cal.get(Calendar.MINUTE) << 5
            | cal.get(Calendar.SECOND) >> 1;
}

From source file:edu.zipcloud.core.util.DateUtil.java

public static Date getEndOfDay(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);//w  w w . ja  va2 s .c o m
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    calendar.set(Calendar.MILLISECOND, 999);
    return calendar.getTime();
}

From source file:Main.java

public static int[] getTimeFields(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);//ww  w.  jav  a 2 s . com
    int[] timeFields = new int[6];
    timeFields[0] = calendar.get(Calendar.YEAR);
    timeFields[1] = calendar.get(Calendar.MONTH);
    timeFields[2] = calendar.get(Calendar.DAY_OF_MONTH);
    timeFields[3] = calendar.get(Calendar.HOUR_OF_DAY);
    timeFields[4] = calendar.get(Calendar.MINUTE);
    timeFields[5] = calendar.get(Calendar.SECOND);
    return timeFields;
}

From source file:Main.java

/**
 * Encode a java date/time into a 16-bit encoded DOS time
 * //  ww  w .  j a v  a  2 s.c o  m
 * @param javaDateTime
 * @return long
 */
public static int encodeTime(long javaDateTime) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(javaDateTime);
    return 2048 * cal.get(Calendar.HOUR_OF_DAY) + 32 * cal.get(Calendar.MINUTE) + cal.get(Calendar.SECOND) / 2;
}

From source file:Main.java

/**
 * Compares (only) the time of two Calendar objects
 * @param firstTime The first calendar object
 * @param secondTime The second calendar object
 * @return -1 if the time of the first calendar object is earlier than the second calendar object,
 *          1 if the time of the second calendar object is earlier than the first calendar object,
 *          0 if the times are equal/*w  w  w  .j  a  v  a  2  s  . c  o  m*/
 */
public static int compareTime(Calendar firstTime, Calendar secondTime) {
    if (firstTime.get(Calendar.HOUR_OF_DAY) < secondTime.get(Calendar.HOUR_OF_DAY)) {
        return -1;
    } else if (firstTime.get(Calendar.HOUR_OF_DAY) > secondTime.get(Calendar.HOUR_OF_DAY)) {
        return 1;
    }
    if (firstTime.get(Calendar.MINUTE) < secondTime.get(Calendar.MINUTE)) {
        return -1;
    } else if (firstTime.get(Calendar.MINUTE) > secondTime.get(Calendar.MINUTE)) {
        return 1;
    }
    return 0;
}

From source file:Main.java

/**
 * Creates a calendar object with the next absolute time determined by hour and minute
 * @param hourOfDay//www.  j  a v a2s. com
 * @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

/**
 * This method returns a new Date with a specified hour, minute, second and millis
 *
 * @param date/*from ww w.  j  av a2 s. c  o  m*/
 *         Start date
 * @param hourOfDay
 *         Target hour (0 - 23)
 * @param minute
 *         Target minute (0 - 59)
 * @param second
 *         Target second (0 - 59)
 * @param millis
 *         Target millis (0 - 999)
 *
 * @return The requested time
 */
public static Date setTime(Date date, int hourOfDay, int minute, int second, int millis) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
    cal.set(Calendar.MINUTE, minute);
    cal.set(Calendar.SECOND, second);
    cal.set(Calendar.MILLISECOND, millis);
    return cal.getTime();
}

From source file:com.wiiyaya.framework.common.utils.DateUtils.java

/**
 * 00:00:00/*from w  w  w.  j  a va 2 s  .c  o m*/
 *
 * @param date 
 * @return ??
 */
public static Date setDayStart(final Date date) {
    final Calendar c = Calendar.getInstance();
    c.setLenient(false);
    c.setTime(date);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    return c.getTime();
}