Example usage for java.util Calendar MILLISECOND

List of usage examples for java.util Calendar MILLISECOND

Introduction

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

Prototype

int MILLISECOND

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

Click Source Link

Document

Field number for get and set indicating the millisecond within the second.

Usage

From source file:com.indoqa.lang.util.TimeUtilsTest.java

@Test
public void getEndOfDay() {
    final DateFormat timeInstance = new SimpleDateFormat("HH:mm:ss.SSS");
    final Random random = new Random();

    Calendar calendar = Calendar.getInstance();

    for (int i = 0; i < 10; i++) {
        calendar.add(Calendar.MILLISECOND, random.nextInt(1000000));
        assertEquals("Failed to calculate end of day for date with long value " + calendar.getTimeInMillis(),
                "23:59:59.999", timeInstance.format(TimeUtils.getEndOfDay(calendar.getTime())));
    }//from www  .  j  av a2 s .  c o m

    timeInstance.setTimeZone(TimeZone.getTimeZone("UTC"));

    assertEquals("21:59:59.999",
            timeInstance.format(TimeUtils.getEndOfDay(new Date(), TimeZone.getTimeZone("GMT+2"))));
    assertEquals("00:59:59.999",
            timeInstance.format(TimeUtils.getEndOfDay(new Date(), TimeZone.getTimeZone("GMT-1"))));
}

From source file:helper.lang.DateHelperTest.java

@Test
public void testGetRangeDaysBeforeTodayUtc() {
    int days = 1;
    Calendar nowUtc = DateHelper.nowUtc();

    CalendarRange range = DateHelper.getRangeFullDaysBefore(nowUtc, days);
    Calendar start = range.getStart();
    Calendar end = range.getEnd();
    System.out.println(DateFormatUtils.SMTP_DATETIME_FORMAT.format(start));
    System.out.println(DateFormatUtils.SMTP_DATETIME_FORMAT.format(end));

    // end date is midnight of date passed in
    assertEquals(nowUtc.get(Calendar.YEAR), end.get(Calendar.YEAR));
    assertEquals(nowUtc.get(Calendar.MONTH), end.get(Calendar.MONTH));
    assertEquals(nowUtc.get(Calendar.DATE), end.get(Calendar.DATE));
    assertEquals(0, end.get(Calendar.HOUR));
    assertEquals(0, end.get(Calendar.MINUTE));
    assertEquals(0, end.get(Calendar.SECOND));
    assertEquals(0, end.get(Calendar.MILLISECOND));
    assertEquals(0, end.get(Calendar.HOUR_OF_DAY));
    assertEquals(DateHelper.UTC_TIME_ZONE, end.getTimeZone());

    // start date is 11:59:59 or 'days' time
    nowUtc.add(Calendar.DATE, -(days + 2));
    assertEquals(nowUtc.get(Calendar.YEAR), start.get(Calendar.YEAR));
    assertEquals(nowUtc.get(Calendar.MONTH), start.get(Calendar.MONTH));
    assertEquals(nowUtc.get(Calendar.DATE), start.get(Calendar.DATE));
    assertEquals(11, start.get(Calendar.HOUR));
    assertEquals(23, start.get(Calendar.HOUR_OF_DAY));
    assertEquals(59, start.get(Calendar.MINUTE));
    assertEquals(59, start.get(Calendar.SECOND));
    assertEquals(999, start.get(Calendar.MILLISECOND));
    assertEquals(DateHelper.UTC_TIME_ZONE, start.getTimeZone());

}

From source file:com.bt.sdk.callcontrol.demo.standup.ConferenceRowCallbackHandler.java

public void processRow(ResultSet arg0) throws SQLException {
    String conferenceName = arg0.getString("name");
    log.debug(conferenceName);// ww  w.  j a  v a  2s.  c  o m

    Calendar conferenceTime = Calendar.getInstance();
    conferenceTime.set(Calendar.HOUR_OF_DAY, arg0.getTime("time").getHours());
    conferenceTime.set(Calendar.MINUTE, arg0.getTime("time").getMinutes());
    conferenceTime.set(Calendar.SECOND, 0);
    conferenceTime.set(Calendar.MILLISECOND, 0);
    String date = DATE_FORMAT.format(Calendar.getInstance().getTime());
    final String key = String.format("%s:%02d%02d:%s", conferenceName, conferenceTime.get(Calendar.HOUR_OF_DAY),
            conferenceTime.get(Calendar.MINUTE), date);
    log.debug(key);
    if (conferenceFutures.containsKey(key)) {
        log.warn(String.format("conference already scheduled: %s", key));
        return;
    }
    final List<String> participants = getParticipants(conferenceName);
    log.debug(participants);
    log.info(String.format("Starting conference \"%s\" in %d seconds", conferenceName,
            secondsBeforeStartTime(conferenceTime)));
    conferenceFutures.put(key, scheduledExecutorService.schedule(new Runnable() {
        public void run() {
            String conferenceId = conferenceBean.createConference(participants.size(),
                    maxConferenceDurationInMinutes);
            conferences.put(conferenceId, key);
            log.debug(conferences);
            for (String participantTelno : participants) {
                String callLegId = conferenceBean.createParticipantCallLeg(conferenceId,
                        URI.create(String.format("sip:%s@%s", participantTelno, pstnGatewayIpAddress)));
                conferenceBean.inviteParticipant(conferenceId, callLegId);
            }
        }
    }, secondsBeforeStartTime(conferenceTime), TimeUnit.SECONDS));
}

From source file:com.greenline.guahao.biz.util.DateUtils.java

/**
 * //from   www  .  j ava2s  .  c  o  m
 * 
 * @param date
 * @return Date
 */
public static Date getYmdTime(Date date) {
    if (date == null) {
        return (new Date());
    }
    Calendar day = Calendar.getInstance();
    day.setTime(date);
    day.set(Calendar.HOUR_OF_DAY, 0);
    day.set(Calendar.MINUTE, 0);
    day.set(Calendar.SECOND, 0);
    day.set(Calendar.MILLISECOND, 0);
    Date convertTime = day.getTime();
    return convertTime;
}

From source file:com.mirth.connect.client.ui.StatusBar.java

private String convertLocalToServerTime() {
    TimeZone localTimeZone = TimeZone.getTimeZone(this.localTimeZone);
    TimeZone serverTimeZone = TimeZone.getTimeZone(this.serverTimeZone);

    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MILLISECOND, (int) timeOffset);

    calendar.add(Calendar.MILLISECOND, localTimeZone.getRawOffset() * -1);
    if (localTimeZone.inDaylightTime(calendar.getTime())) {
        calendar.add(Calendar.MILLISECOND, localTimeZone.getDSTSavings() * -1);
    }//ww w. ja v  a2  s . c om

    calendar.add(Calendar.MILLISECOND, serverTimeZone.getRawOffset());
    if (serverTimeZone.inDaylightTime(calendar.getTime())) {
        calendar.add(Calendar.MILLISECOND, serverTimeZone.getDSTSavings());
    }

    return new SimpleDateFormat("h:mm a").format(calendar.getTime());
}

From source file:org.yamj.core.service.tasks.ExecutionTaskService.java

public void executeTask(ExecutionTask executionTask) {
    ITask task = registeredTasks.get(executionTask.getTaskName().toLowerCase());

    if (task == null) {
        LOG.warn("Task " + executionTask.getTaskName() + " not registered");
    } else {/*from   ww w  . j  a  v  a2 s .c  om*/
        try {
            task.execute(executionTask.getOptions());
        } catch (Exception ex) {
            LOG.error("Failed to execute task '" + task.getTaskName() + "'", ex);
        }
    }

    if (IntervalType.ONCE == executionTask.getIntervalType()) {
        // just delete the task after executed once
        try {
            this.executionTaskStorageService.deleteEntity(executionTask);
        } catch (Exception ex) {
            LOG.error("Failed to delete execution task: " + executionTask.getName(), ex);
        }
        // nothing to do for this task anymore
        return;
    }

    try {
        Calendar nextCal = Calendar.getInstance();
        nextCal.setTime(executionTask.getNextExecution());

        Calendar cal = Calendar.getInstance();
        if (IntervalType.MONTHLY == executionTask.getIntervalType()) {
            cal.add(Calendar.MONTH, 1);
            cal.set(Calendar.DAY_OF_MONTH, nextCal.get(Calendar.DAY_OF_MONTH));
            cal.set(Calendar.HOUR_OF_DAY, nextCal.get(Calendar.HOUR_OF_DAY));
            cal.set(Calendar.MINUTE, nextCal.get(Calendar.MINUTE));
            cal.set(Calendar.SECOND, nextCal.get(Calendar.SECOND));
            cal.set(Calendar.MILLISECOND, 0);
        } else if (IntervalType.DAILY == executionTask.getIntervalType()) {
            cal.add(Calendar.DAY_OF_MONTH, 1);
            cal.set(Calendar.HOUR_OF_DAY, nextCal.get(Calendar.HOUR_OF_DAY));
            cal.set(Calendar.MINUTE, nextCal.get(Calendar.MINUTE));
            cal.set(Calendar.SECOND, nextCal.get(Calendar.SECOND));
            cal.set(Calendar.MILLISECOND, 0);
        } else if (IntervalType.DAYS == executionTask.getIntervalType()) {
            cal.add(Calendar.DAY_OF_MONTH, executionTask.getDelay());
            cal.set(Calendar.HOUR_OF_DAY, nextCal.get(Calendar.HOUR_OF_DAY));
            cal.set(Calendar.MINUTE, nextCal.get(Calendar.MINUTE));
            cal.set(Calendar.SECOND, nextCal.get(Calendar.SECOND));
            cal.set(Calendar.MILLISECOND, 0);
        } else if (IntervalType.HOURS == executionTask.getIntervalType()) {
            cal.add(Calendar.HOUR_OF_DAY, executionTask.getDelay());
            cal.set(Calendar.MINUTE, nextCal.get(Calendar.MINUTE));
            cal.set(Calendar.SECOND, nextCal.get(Calendar.SECOND));
            cal.set(Calendar.MILLISECOND, 0);
        } else if (IntervalType.MINUTES == executionTask.getIntervalType()) {
            cal.add(Calendar.MINUTE, executionTask.getDelay());
            cal.set(Calendar.SECOND, nextCal.get(Calendar.SECOND));
            cal.set(Calendar.MILLISECOND, 0);
        }

        executionTask.setLastExecution(new Date());
        executionTask.setNextExecution(cal.getTime());
        this.executionTaskStorageService.updateEntity(executionTask);
    } catch (Exception ex) {
        LOG.error("Failed to update: {}", executionTask);
        LOG.warn("Storage error", ex);
    }
}

From source file:de.micromata.genome.util.types.DateUtils.java

/**
 * Arbeitet ber die Millisekunden./*  www .  j a  v  a  2  s .c om*/
 *
 * @param date Nie <code>null</code>.
 * @param millis Positive und negative Zahlen erlaubt. Muss im IntRange-Bereich liegen!
 * @return Das neue Datum.
 */
public static Date addTics(final Date date, final long millis) {
    if (millis < Integer.MIN_VALUE || Integer.MAX_VALUE < millis) {
        throw new IllegalArgumentException("millis out of range: " + millis);
    }
    final Calendar cal = getCalendarInstance();
    cal.setTime(date);
    cal.add(Calendar.MILLISECOND, (int) millis);
    final Date result = cal.getTime();
    return result;
}

From source file:com.lfv.yada.net.server.ServerLogger.java

public synchronized void print(String s) {
    if (printer != null && s != null && suspendTime == 0) {
        long t = System.currentTimeMillis();

        // UTC time
        calendar.setTimeInMillis(t);/*from  w  w w  .  j a v a 2 s .com*/
        String st = "UTC " + s2(calendar.get(Calendar.HOUR_OF_DAY)) + ":" + s2(calendar.get(Calendar.MINUTE))
                + ":" + s2(calendar.get(Calendar.SECOND)) + "." + s3(calendar.get(Calendar.MILLISECOND));

        // Simulation time
        calendar.setTimeInMillis(resumeTime + t - startTime);
        st += " SIM " + s2(calendar.get(Calendar.HOUR_OF_DAY)) + ":" + s2(calendar.get(Calendar.MINUTE)) + ":"
                + s2(calendar.get(Calendar.SECOND)) + "." + s3(calendar.get(Calendar.MILLISECOND));

        // Print it to the output logfile
        printer.println(st + " | " + s);
    }
}

From source file:com.adobe.acs.commons.http.headers.impl.AbstractExpiresHeaderFilterTest.java

@Test
public void testGetHeaderValue() throws Exception {

    when(componentContext.getProperties()).thenReturn(properties);

    Calendar expected = Calendar.getInstance();
    expected.set(Calendar.HOUR_OF_DAY, 2);
    expected.set(Calendar.MINUTE, 30);
    expected.set(Calendar.SECOND, 0);
    expected.set(Calendar.MILLISECOND, 0);

    filter.doActivate(componentContext);
    String header = filter.getHeaderValue();
    Date date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z").parse(header);
    Calendar actual = Calendar.getInstance();
    actual.setTime(date);//from   ww  w  . ja  v a2s .  c  o  m
    actual.set(Calendar.SECOND, 0);
    actual.set(Calendar.MILLISECOND, 0);

    assertTrue(DateUtils.isSameInstant(expected, actual));
}

From source file:com.ancientprogramming.fixedformat4j.format.impl.TestFixedFormatManagerImpl.java

private MyRecord createMyRecord() {
    Calendar someDay = Calendar.getInstance();
    someDay.set(2008, 4, 14, 0, 0, 0);/*from   w  ww . j av a  2  s .com*/
    someDay.set(Calendar.MILLISECOND, 0);

    MyRecord myRecord = new MyRecord();
    myRecord.setBooleanData(true);
    myRecord.setCharData('C');
    myRecord.setDateData(someDay.getTime());
    myRecord.setDoubleData(10.35);
    myRecord.setFloatData(20.56F);
    myRecord.setLongData(11L);
    myRecord.setIntegerData(123);
    myRecord.setStringData("some text ");
    myRecord.setBigDecimalData(new BigDecimal(-12.012));
    myRecord.setSimpleFloatData(20.56F);
    return myRecord;
}