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 setDateTime(Date originalDate, int hour, int minute, int second) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(originalDate);/*from   w  ww  .  j  ava2  s.  c o  m*/
    cal.set(Calendar.HOUR, hour);
    cal.set(Calendar.MINUTE, minute);
    cal.set(Calendar.SECOND, second);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
}

From source file:Main.java

/**
 * Returns milliseconds of the date argument dt. If the argument
 * isIncludeTime is false then the returned milliseconds does not include
 * time.//w w w  .  ja v a2 s . c o  m
 * 
 * @param dt
 * @param isIncludeTime
 * @return
 */
private static long getDate(Date dt, boolean isIncludeTime) {
    Calendar cal = GregorianCalendar.getInstance();
    cal.setTime(dt);
    if (!isIncludeTime) {
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
    }
    return cal.getTimeInMillis();
}

From source file:is.idega.idegaweb.egov.gumbo.licenses.SetDraganotveidiValidPeriod.java

public void execute(ExecutionContext executionContext) throws Exception {

    final Interval period;

    final Calendar now = Calendar.getInstance();
    now.set(Calendar.HOUR, 0);
    now.set(Calendar.MINUTE, 0);/* w  w  w  . j av a 2 s  .c  o m*/
    now.set(Calendar.SECOND, 0);
    now.set(Calendar.MILLISECOND, 0);

    final Calendar mayStart = Calendar.getInstance();
    mayStart.set(now.get(Calendar.YEAR), Calendar.MAY, 1, 0, 0, 0);

    final Calendar augEnd = Calendar.getInstance();
    augEnd.set(now.get(Calendar.YEAR), Calendar.AUGUST, 31, 0, 0, 0);

    if (now.after(mayStart) && now.before(augEnd)) {

        period = new Interval(now.getTime(), augEnd.getTime());

    } else {

        period = findNearestPeriod(now);
    }

    executionContext.setVariable("date_validityFrom", period.getFrom());
    executionContext.setVariable("date_validityTo", period.getTo());
}

From source file:TimeUtil.java

public static String stringSecsFormat(long msecs) {
    GregorianCalendar cal = new GregorianCalendar();
    StringBuffer sBuf = new StringBuffer(11);

    cal.setTime(new Date(msecs));

    int hour = cal.get(Calendar.HOUR);

    if (hour == 0)
        hour = 12;//from w  w  w .j  a  va 2 s .c  om

    if (hour < 10)
        sBuf.append(" ");

    sBuf.append(Integer.toString(hour));
    sBuf.append(":");

    int minute = cal.get(Calendar.MINUTE);

    if (minute < 10)
        sBuf.append("0");

    sBuf.append(Integer.toString(minute));
    sBuf.append(":");

    int secs = cal.get(Calendar.SECOND);

    if (secs < 10) {
        sBuf.append("0");
    }
    sBuf.append(Integer.toString(secs));

    sBuf.append(" ");
    sBuf.append(cal.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM");

    return (sBuf.toString());
}

From source file:cz.cvut.kbss.wpa.service.test.PlayerServiceTest.java

@BeforeClass
public static void setUp() {
    b = Calendar.getInstance();/*from  w ww  . j ava  2  s .c o m*/
    b.set(Calendar.YEAR, 1986);
    b.set(Calendar.MONTH, Calendar.APRIL);
    b.set(Calendar.DAY_OF_MONTH, 28);
    b.set(Calendar.HOUR, 0);
    b.set(Calendar.MINUTE, 0);
    b.set(Calendar.SECOND, 0);
}

From source file:J2MEFixedRateSchedule.java

public void startApp() {
    d.addCommand(new Command("Exit", Command.EXIT, 0));
    d.setCommandListener(new CommandListener() {
        public void commandAction(Command c, Displayable s) {
            notifyDestroyed();//from   w  w w.  ja  v a 2  s . c o  m
        }
    });
    now.setTime(currentTime);

    nowString = now.get(Calendar.HOUR) + ":" + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND) + ":";

    aTimer = new Timer();
    aTimerTask = new ClockTimerTask();
    aTimer.scheduleAtFixedRate(aTimerTask, 10, 1000);

    Display.getDisplay(this).setCurrent(d);
}

From source file:helper.lang.DateHelperTest.java

@Test
public void testAsUtcDayCalendar() {
    Calendar localTime = Calendar.getInstance(TimeZone.getTimeZone("CST"));
    Calendar cal = DateHelper.asUtcDay(localTime);
    assertEquals(0, cal.get(Calendar.MILLISECOND));
    assertEquals(0, cal.get(Calendar.MINUTE));
    assertEquals(0, cal.get(Calendar.HOUR));
    assertEquals(0, cal.get(Calendar.HOUR_OF_DAY));
    assertEquals(DateHelper.UTC_TIME_ZONE, cal.getTimeZone());
    assertEquals(TimeZone.getTimeZone("CST"), localTime.getTimeZone());
    assertEquals(localTime.get(Calendar.YEAR), cal.get(Calendar.YEAR));
    assertEquals(localTime.get(Calendar.MONTH), cal.get(Calendar.MONTH));
    assertEquals(localTime.get(Calendar.DATE), cal.get(Calendar.DATE));
}

From source file:com.wanikani.wklib.UserInformation.java

private static Calendar getNormalizedCalendar(Date date) {
    Calendar ans;//from w  w  w  . j a v  a  2  s . c  o  m

    ans = Calendar.getInstance();
    ans.setTime(date);
    ans.set(Calendar.HOUR, 1);
    ans.set(Calendar.MINUTE, 2);
    ans.set(Calendar.SECOND, 3);
    ans.set(Calendar.MILLISECOND, 4);

    return ans;
}

From source file:com.appleframework.monitor.model.TimeRangeTest.java

public void test_start() throws Exception {
    timeRange.setNow(DateUtils.parseDate("2011-11-11 00:00", new String[] { DATE_FORMAT }));
    timeRange.setLast(1);//from   w ww .  j  a v  a  2  s .  com
    timeRange.setUnit(Calendar.HOUR);
    assertEquals("2011-11-10 23:00", sdf.format(timeRange.getStart()));
    timeRange.setUnit(Calendar.DATE);
    assertEquals("2011-11-10 00:00", sdf.format(timeRange.getStart()));
}

From source file:SampleLang.java

public static void checkDate() throws InterruptedException, ParseException {
    //date1 created
    Date date1 = new Date();
    //Print the date and time at this instant
    System.out.println("The time right now is >>" + date1);
    //Thread sleep for 1000 ms
    Thread.currentThread().sleep(DateUtils.MILLIS_PER_MINUTE);
    //date2 created.
    Date date2 = new Date();
    //Check if date1 and date2 have the same day
    System.out.println("Is Same Day >> " + DateUtils.isSameDay(date1, date2));
    //Check if date1 and date2 have the same instance
    System.out.println("Is Same Instant >> " + DateUtils.isSameInstant(date1, date2));
    //Round the hour
    System.out.println("Date after rounding >>" + DateUtils.round(date1, Calendar.HOUR));
    //Truncate the hour
    System.out.println("Date after truncation >>" + DateUtils.truncate(date1, Calendar.HOUR));
    //Three dates in three different formats
    String[] dates = { "2005.03.24 11:03:26", "2005-03-24 11:03", "2005/03/24" };
    //Iterate over dates and parse strings to java.util.Date objects
    for (int i = 0; i < dates.length; i++) {
        Date parsedDate = DateUtils.parseDate(dates[i],
                new String[] { "yyyy/MM/dd", "yyyy.MM.dd HH:mm:ss", "yyyy-MM-dd HH:mm" });
        System.out.println("Parsed Date is >>" + parsedDate);
    }//from   w  w w . jav  a 2  s  . co m
    //Display date in HH:mm:ss format
    System.out.println("Now >>" + DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(System.currentTimeMillis()));
}