Example usage for java.util Calendar HOUR

List of usage examples for java.util Calendar HOUR

Introduction

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

Prototype

int HOUR

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

Click Source Link

Document

Field number for get and set indicating the hour of the morning or afternoon.

Usage

From source file:Main.java

public static Date buildShiftStartTime(int startHour, int startMin, int startAmPm, int endHour, int endMin,
        int endAmPm) {
    startHour = startHour == 12 ? 0 : startHour;

    calendar.clear();/* w w w.  j a v a  2  s  .c o  m*/

    calendar.set(Calendar.HOUR, startHour);
    calendar.set(Calendar.MINUTE, startMin);

    calendar.set(Calendar.AM_PM, startAmPm);

    return calendar.getTime();
}

From source file:Util.java

/**
 * Utility for setting the time on a date.
 * @param date//from w w  w .  j a  v a 2s . co  m
 * @param h
 * @param m
 * @param s
 * @return the date with the time set
 */
public static Date setTime(Date date, int h, int m, int s) {
    if (date == null)
        return null;
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.set(Calendar.HOUR, h);
    calendar.set(Calendar.MINUTE, m);
    calendar.set(Calendar.SECOND, s);
    return calendar.getTime();
}

From source file:Main.java

public static String buildShiftTimeRepresentation(Date shiftTime) {
    calendar.setTime(shiftTime);/*from w w w  .  ja va2  s .com*/

    String s = "";
    s = format.format(calendar.get(Calendar.HOUR) == 0 ? 12 : calendar.get(Calendar.HOUR));
    s += ":" + format.format(calendar.get(Calendar.MINUTE));
    s += calendar.get(Calendar.AM_PM) == Calendar.AM ? " AM" : " PM";
    return s;
}

From source file:Main.java

/**
 * Add the given amount of hours to the given calendar. The changes are reflected in the given
 * calendar./* w w  w.  ja va 2 s .  co m*/
 * @param calendar The calendar to add the given amount of hours to.
 * @param hours The amount of hours to be added to the given calendar. Negative values are also
 * allowed, it will just go back in time.
 */
public static void addHours(Calendar calendar, int hours) {
    calendar.add(Calendar.HOUR, hours);
}

From source file:Main.java

public static String genererNomFichierInexistant(String directory, String extensionFichier) {
    Calendar c = Calendar.getInstance();
    File aRetourner;/*w ww .  j  a  v a2  s.  c  o m*/
    if (extensionFichier.equals("")) {
        aRetourner = new File(
                c.get(Calendar.DAY_OF_MONTH) + "_" + c.get(Calendar.MONTH) + "_" + c.get(Calendar.YEAR) + "_"
                        + c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND));
    } else if (directory.equals("")) {
        aRetourner = new File(c.get(Calendar.DAY_OF_MONTH) + "_" + c.get(Calendar.MONTH) + "_"
                + c.get(Calendar.YEAR) + "_" + c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE) + ":"
                + c.get(Calendar.SECOND) + "." + extensionFichier);
    } else {
        aRetourner = new File(directory + "/" + c.get(Calendar.DAY_OF_MONTH) + "_" + c.get(Calendar.MONTH) + "_"
                + c.get(Calendar.YEAR) + "_" + c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE) + ":"
                + c.get(Calendar.SECOND) + "." + extensionFichier);
    }

    return aRetourner.getPath();
}

From source file:Main.java

public static String getDateFromUTC(long timestamp) {
    if (timestamp == 0)
        timestamp = System.currentTimeMillis();
    Date date = new Date(timestamp);
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
    cal.setTime(date);/*from   ww w.  ja va 2s .c o  m*/
    String val = (cal.get(Calendar.MONTH) + "/" + cal.get(Calendar.DATE) + " " + cal.get(Calendar.HOUR) + ":"
            + cal.get(Calendar.MINUTE) + (cal.get(Calendar.AM_PM) == 0 ? "AM" : "PM"));

    //      final Date currentTime = new Date();
    //      final SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy hh:mm:ss a z");
    //      sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    //      String ret = "UTC time: " + sdf.format(currentTime);
    //      System.out.println(ret);

    return val;
}

From source file:Main.java

/**
 * Takes in a duration in minutes and a start timestamp and returns
 * the formatted time that is timestamp + duration
 */// w  ww. j  a  v a  2  s .c  o m
public static String getTimeAfterStart(long startTime, int duration) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(startTime);
    calendar.add(Calendar.MINUTE, duration);

    int minute = calendar.get(Calendar.MINUTE);
    String minuteString = "";
    if (minute < 10)
        minuteString = "0" + minute;
    else
        minuteString = "" + minute;
    String timeString = calendar.get(Calendar.HOUR) + ":" + minuteString + " "
            + AM_PM[calendar.get(Calendar.AM_PM)];
    return timeString;
}

From source file:Main.java

static Calendar toNextWholeHour(Calendar d) {
    Calendar c = new GregorianCalendar();
    c.setTime(d.getTime());/*from  w  ww . j  a  va2 s. c om*/
    c.add(Calendar.HOUR, 1);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    return c;
}

From source file:Main.java

public static Date truncateDate(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);/*from  w w w .  j a  v  a2  s  . c o m*/
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
}

From source file:Main.java

public static void trunkDay(Calendar c) {
    c.set(Calendar.HOUR, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
}