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

/**
 * getDateEnd//from   w w w .  j a  v  a2 s. c o  m
 * @param date the date that needs to be converted to an end date
 * @return a date at 11:59 PM
 */
public static Date getDateEnd(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    cal.set(Calendar.MILLISECOND, 59);
    long time = cal.getTimeInMillis();
    date.setTime(time);
    return date;
}

From source file:Main.java

public static long getStartOfMonthMs(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.getActualMinimum(Calendar.DAY_OF_MONTH));
    date.set(Calendar.HOUR_OF_DAY, date.getActualMinimum(Calendar.HOUR_OF_DAY));
    date.set(Calendar.MINUTE, date.getActualMinimum(Calendar.MINUTE));
    date.set(Calendar.SECOND, date.getActualMinimum(Calendar.SECOND));
    date.set(Calendar.MILLISECOND, date.getActualMinimum(Calendar.MILLISECOND));
    return date.getTimeInMillis();
}

From source file:Main.java

/**
 * Get the SunRise for the given date//from w  w  w . j  a  va  2 s  . c  om
 * @param date - input date
 * @return - Date with time specifying the Sun Rise
 */
public static Date getSunRise(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, 06);
    cal.set(Calendar.MINUTE, 00);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    Date ret = cal.getTime();
    return ret;
}

From source file:Main.java

public static Date addHours(Date date, int hours) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);/*ww w . j  av  a2  s  .  c  om*/
    cal.add(Calendar.HOUR_OF_DAY, hours);
    return cal.getTime();
}

From source file:Main.java

/**
 * Remove the time of a date value/*from w  w  w  .j av  a 2  s . com*/
 *
 * @param date Date to remove the time part
 * @return A date with its time set to 00:00:00
 */
public static Date removeTime(Date date) {
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(date);
    gc.set(Calendar.HOUR_OF_DAY, 0);
    gc.set(Calendar.MINUTE, 0);
    gc.set(Calendar.SECOND, 0);
    gc.set(Calendar.MILLISECOND, 0);
    return gc.getTime();
}

From source file:Main.java

public static Date parseData(String text) {
    Calendar calendar = Calendar.getInstance();
    if (text.contains(":")) {
        String[] time = text.split(":");
        int hours = Integer.parseInt(time[0]);
        calendar.set(Calendar.MINUTE, Integer.parseInt(time[1]));
        calendar.set(Calendar.HOUR_OF_DAY, hours);
        return calendar.getTime();
    } else if (text.contains("/")) {
        String[] date = text.split("/");
        if (date.length == 3) {
            calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date[0]));
            calendar.set(Calendar.MONTH, Integer.parseInt(date[1]) - 1);
            calendar.set(Calendar.YEAR, Integer.parseInt(date[2]));
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            return calendar.getTime();
        } else if (date.length == 2) {
            calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date[0]));
            calendar.set(Calendar.MONTH, Integer.parseInt(date[1]) - 1);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            return calendar.getTime();
        }//  ww w . j a va 2s .  c o m
    }
    return null;
}

From source file:Main.java

public static String getCurrentTimeStampHHMM() {
    Calendar cal = new GregorianCalendar();
    return makeTimeString(cal.get(Calendar.HOUR_OF_DAY), 2) + ":" + makeTimeString(cal.get(Calendar.MINUTE), 2);
}

From source file:Main.java

public static String getCurrentTimeStampHHMMSS() {
    Calendar cal = new GregorianCalendar();
    return makeTimeString(cal.get(Calendar.HOUR_OF_DAY), 2) + ":" + makeTimeString(cal.get(Calendar.MINUTE), 2)
            + ":" + makeTimeString(cal.get(Calendar.SECOND), 2);
}

From source file:Main.java

public static String getCurrentTime() {
    DecimalFormat df = new DecimalFormat("00");
    Calendar c = Calendar.getInstance();
    String currentTime = c.get(Calendar.YEAR)// + "-"
            + df.format((c.get(Calendar.MONTH) + 1)) // + "-"
            + df.format(c.get(Calendar.DAY_OF_MONTH))// + "-"
            + df.format(c.get(Calendar.HOUR_OF_DAY)) // + "-"
            + df.format(c.get(Calendar.MINUTE))// + "-"
            + df.format(c.get(Calendar.SECOND));
    return currentTime;
}

From source file:Main.java

/**
 * Returns current date-time in format of yyyy-MM-dd hh:mm:ss
 * //from  w  w w  . java2 s .  c o  m
 * @return date time in string.
 */
public static String getCurrentTime() {
    int yyyy = Calendar.getInstance().get(Calendar.YEAR);
    int MM = Calendar.getInstance().get(Calendar.MONTH) + 1;
    int dd = Calendar.getInstance().get(Calendar.DATE);
    int hh = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    int mm = Calendar.getInstance().get(Calendar.MINUTE);
    int ss = Calendar.getInstance().get(Calendar.SECOND);
    String curDate = String.format("%04d-%02d-%02d %02d:%02d:%02d", yyyy, MM, dd, hh, mm, ss);

    return curDate;

}