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

/**
 * Formats a date/time value as a string.
 * //from   w  w  w .  j ava  2s  . c o m
 * @param date the date to format
 * @return the formatted date
 */
public static String formatDateTime(Calendar date) {
    StringBuilder builder = new StringBuilder(16);
    appendField(builder, date.get(Calendar.YEAR), 4);
    appendField(builder, date.get(Calendar.MONTH) + 1, 2);
    appendField(builder, date.get(Calendar.DAY_OF_MONTH), 2);
    appendField(builder, date.get(Calendar.HOUR_OF_DAY), 2);
    appendField(builder, date.get(Calendar.MINUTE), 2);
    appendField(builder, date.get(Calendar.SECOND), 2);
    return builder.toString();
}

From source file:Main.java

public static int compare(Calendar date1, Calendar date2) {
    int i = date1.get(Calendar.YEAR);
    int j = date2.get(Calendar.YEAR);

    if (i > j)
        return 1;
    if (i < j)
        return -1;

    i = date1.get(Calendar.MONTH);
    j = date2.get(Calendar.MONTH);
    if (i > j)
        return 1;
    if (i < j)
        return -1;

    i = date1.get(Calendar.DAY_OF_MONTH);
    j = date2.get(Calendar.DAY_OF_MONTH);
    if (i > j)
        return 1;
    if (i < j)
        return -1;

    i = date1.get(Calendar.HOUR_OF_DAY);
    j = date2.get(Calendar.HOUR_OF_DAY);
    if (i > j)
        return 1;
    if (i < j)
        return -1;

    i = date1.get(Calendar.MINUTE);
    j = date2.get(Calendar.MINUTE);
    if (i > j)
        return 1;
    if (i < j)
        return -1;

    i = date1.get(Calendar.SECOND);
    j = date2.get(Calendar.SECOND);
    if (i > j)
        return 1;
    if (i < j)
        return -1;

    return 0;//w w w  .ja v  a 2  s. c o  m
}

From source file:Main.java

/**
 * Packs java time value into an MS-DOS time value.
 * @param time the time value//  w ww .ja va  2 s.  c om
 * @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:Util.java

public static byte[] packDate(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);/*from w w  w.  j a v a 2s  .  com*/
    // Byte bits: 00000000 11111111 22222222 33333333 44444444
    // Contents : 00YYYYYY YYYYYYMM MMDDDDDH HHHHMMMM MMSSSSSS
    byte[] bytes = new byte[5];
    int s = c.get(Calendar.SECOND);
    int m = c.get(Calendar.MINUTE);
    int h = c.get(Calendar.HOUR_OF_DAY);
    int d = c.get(Calendar.DATE);
    int mm = c.get(Calendar.MONTH) + 1;
    int y = c.get(Calendar.YEAR);

    bytes[4] = (byte) ((m << 6) | s);
    bytes[3] = (byte) ((m >> 2) | (h << 4));
    bytes[2] = (byte) ((h >> 4) | (d << 1) | (mm << 6));
    bytes[1] = (byte) ((mm >> 2) | (y << 2));
    bytes[0] = (byte) (y >> 6);

    return bytes;
}

From source file:Main.java

private static void setStartOfDayToCalendar(Calendar c) {
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
}

From source file:Main.java

/***
 * Fetch the estimate current time of arrival at the destination
 * @param timeZone - The timezone at the destination
 * @param distance - how far to the target
 * @param speed - how fast we are moving
 * @return String - "HH:MM" current time at the target
 *//*  w w  w. jav a2 s.  c  o m*/
public static String calculateEta(TimeZone timeZone, double distance, double speed) {

    // If no speed, then return an empty display string
    if (0 == speed) {
        return "--:--";
    }

    // fetch the raw ETE
    Time eteRaw = fetchRawEte(distance, speed, 0, true);

    // If the eteRaw is meaningless, then return an empty display string
    if (null == eteRaw) {
        return "--:--";
    }

    // Break the hours and minutes out
    int eteHr = eteRaw.hour;
    int eteMin = eteRaw.minute;

    // Hours greater than 99 are not displayable
    if (eteHr > 99) {
        return "XX:XX";
    }

    // Get the current local time hours and minutes
    int etaHr = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    int etaMin = Calendar.getInstance().get(Calendar.MINUTE);

    // Add in our ETE to the current time, accounting for rollovers
    etaMin += eteMin; // Add the estimated minutes enroute to "now"
    if (etaMin > 59) {
        etaMin -= 60;
        etaHr++;
    } // account for minute rollover
    etaHr += eteHr; // Now add the hours enroute
    while (etaHr > 23) {
        etaHr -= 24;
    } // account for midnight rollover

    // Format the hours and minutes
    String strHr = String.format(Locale.getDefault(), "%02d", etaHr);
    String strMn = String.format(Locale.getDefault(), "%02d", etaMin);

    // Build string of return
    return strHr + ":" + strMn;
}

From source file:Main.java

/***
 * Fetch the estimate current time of arrival at the destination
 * @param timeZone - The timezone at the destination
 * @param distance - how far to the target
 * @param speed - how fast we are moving
 * @param bearing - direction to target/*from  www .j  ava 2 s.c o m*/
 * @param heading - direction of movement
 * @return String - "HH:MM" current time at the target
 */
public static String calculateEta(TimeZone timeZone, double distance, double speed, double bearing,
        double heading) {

    // fetch the raw ETE
    int eteRaw = fetchRawEte(distance, speed, bearing, heading);

    // If no speed, or the eteRaw is meaningless, then return an empty display string
    if (0 == speed || eteRaw == -1) {
        return "--:--";
    }

    // Break the hours and minutes out
    int eteHr = eteRaw / 100;
    int eteMin = eteRaw % 100;

    // Hours greater than 99 are not displayable
    if (eteHr > 99) {
        return "XX:XX";
    }

    // Get the current local time hours and minutes
    int etaHr = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    int etaMin = Calendar.getInstance().get(Calendar.MINUTE);

    // Add in our ETE to the current time, accounting for rollovers
    etaMin += eteMin; // Add the estimated minutes enroute to "now"
    if (etaMin > 59) {
        etaMin -= 60;
        etaHr++;
    } // account for minute rollover
    etaHr += eteHr; // Now add the hours enroute
    while (etaHr > 23) {
        etaHr -= 24;
    } // account for midnight rollover

    // Format the hours and minutes
    String strHr = String.format(Locale.getDefault(), "%02d", etaHr);
    String strMn = String.format(Locale.getDefault(), "%02d", etaMin);

    // Build string of return
    return strHr + ":" + strMn;
}

From source file:Main.java

/**
 * Decode a 16-bit encoded DOS date/time into a java date/time.
 * //from w ww .j a v  a2  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();
}