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.silverpeas.calendar.DateTimeTest.java

@Test
public void createsAtASpecifiedDateTime() {
    Calendar now = getInstance();
    DateTime expected = new DateTime(now.getTime());
    DateTime actual = DateTime.dateTimeAt(now.get((Calendar.YEAR)), now.get(Calendar.MONTH),
            now.get(Calendar.DAY_OF_MONTH), now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE),
            now.get(Calendar.SECOND), now.get(Calendar.MILLISECOND));
    assertEquals(expected.getTime(), actual.getTime(), 100);
}

From source file:Main.java

/**
 * Parse a date from ISO-8601 formatted string. It expects a format
 * yyyy-MM-ddThh:mm:ss[.sss][Z|[+-]hh:mm]
 *
 * @param date ISO string to parse in the appropriate format.
 * @return the parsed date//from w  ww  .j a  v  a2  s.  c o m
 * @throws IllegalArgumentException if the date is not in the appropriate format
 */
public static Date parse(String date) {
    try {
        int offset = 0;

        // extract year
        int year = parseInt(date, offset, offset += 4);
        checkOffset(date, offset, '-');

        // extract month
        int month = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, '-');

        // extract day
        int day = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, 'T');

        // extract hours, minutes, seconds and milliseconds
        int hour = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, ':');

        int minutes = parseInt(date, offset += 1, offset += 2);
        checkOffset(date, offset, ':');

        int seconds = parseInt(date, offset += 1, offset += 2);
        // milliseconds can be optional in the format
        // always use 0 otherwise returned date will include millis of current time
        int milliseconds = 0;

        if (date.charAt(offset) == '.') {
            checkOffset(date, offset, '.');
            int digitCount = 1;
            while (offset + digitCount < date.length() && digitCount < 3
                    && date.charAt(offset + 1 + digitCount) != 'Z'
                    && date.charAt(offset + 1 + digitCount) != '+'
                    && date.charAt(offset + 1 + digitCount) != '-') {
                digitCount++;
            }
            String msString = date.substring(offset += 1, offset += digitCount);
            while (msString.length() < 3) {
                msString += '0';
            }
            milliseconds = parseInt(msString, 0, 3);
        }

        // extract timezone
        String timezoneId = null;
        while (offset < date.length()) {
            char timezoneIndicator = date.charAt(offset);
            if (timezoneIndicator == '+' || timezoneIndicator == '-') {
                timezoneId = GMT_ID + date.substring(offset);
                break;
            } else if (timezoneIndicator == 'Z') {
                timezoneId = GMT_ID;
                break;
            }
            offset++;
        }
        if (timezoneId == null) {
            throw new IndexOutOfBoundsException("Invalid time zone indicator ");
        }
        TimeZone timezone = TimeZone.getTimeZone(timezoneId);
        if (!timezone.getID().equals(timezoneId)) {
            throw new IndexOutOfBoundsException();
        }

        Calendar calendar = new GregorianCalendar(timezone);
        calendar.setLenient(false);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minutes);
        calendar.set(Calendar.SECOND, seconds);
        calendar.set(Calendar.MILLISECOND, milliseconds);

        return calendar.getTime();
    } catch (IndexOutOfBoundsException | IllegalArgumentException e) {
        throw new IllegalArgumentException("Failed to parse date " + date, e);
    }
}

From source file:helper.util.DateHelper.java

/**
 * Take a local time calendar and represent it as a day
 * (truncate to day, retain yy/mm/dd from original date)
 **///  w w  w . ja v  a 2  s. co  m
public static Calendar asDay(Calendar localTimeCalendar) {
    Calendar asDay = Calendar.getInstance(localTimeCalendar.getTimeZone());
    asDay.clear();
    asDay.set(localTimeCalendar.get(Calendar.YEAR), localTimeCalendar.get(Calendar.MONTH),
            localTimeCalendar.get(Calendar.DATE), 0, 0, 0);
    asDay.set(Calendar.AM_PM, Calendar.AM);
    asDay.set(Calendar.MILLISECOND, 0);
    asDay.set(Calendar.HOUR, 0);
    return asDay;
}

From source file:com.haulmont.chile.core.datatypes.impl.DateDatatype.java

protected java.sql.Date normalize(java.util.Date dateTime) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(dateTime);/*from  w ww .  j  av a2  s .co  m*/
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return new java.sql.Date(cal.getTimeInMillis());
}

From source file:com.evolveum.midpoint.repo.sql.CleanupTest.java

License:asdf

private Calendar create_2013_07_12_12_00_Calendar() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, 2013);
    calendar.set(Calendar.MONTH, Calendar.MAY);
    calendar.set(Calendar.DAY_OF_MONTH, 7);
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    return calendar;
}

From source file:com.mycompany.craftdemo.utility.java

public static boolean isValidTime() {
    Calendar calendar = Calendar.getInstance();
    TimeZone fromTimeZone = calendar.getTimeZone();
    TimeZone toTimeZone = TimeZone.getTimeZone("EST");

    calendar.setTimeZone(fromTimeZone);/*from  www . ja v a2 s . c  om*/
    calendar.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1);
    if (fromTimeZone.inDaylightTime(calendar.getTime())) {
        calendar.add(Calendar.MILLISECOND, calendar.getTimeZone().getDSTSavings() * -1);
    }

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

    int hour = calendar.get(Calendar.HOUR_OF_DAY);

    return hour >= 9 && hour <= 16;
}

From source file:emily.util.YTUtil.java

/**
 * Time until the next google api reset happens (Midnight PT), or 9am GMT
 *
 * @return formatted string, eg. "10 minutes form now"
 *//*  w  ww  .j av a  2 s  .c  o m*/
public static String nextApiResetTime() {
    Calendar c = Calendar.getInstance();
    c.add(Calendar.DAY_OF_MONTH, 0);
    c.set(Calendar.HOUR_OF_DAY, 9);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    return TimeUtil.getRelativeTime(
            (System.currentTimeMillis()
                    + (c.getTimeInMillis() - System.currentTimeMillis()) % TimeUnit.DAYS.toMillis(1)) / 1000L,
            false);
}

From source file:HSSFDateUtil.java

/**
 * Given a Date, converts it into a double representing its internal Excel
 * representation, which is the number of days since 1/1/1900. Fractional days
 * represent hours, minutes, and seconds.
 * /*from   ww  w .j av  a  2  s  . c om*/
 * @return Excel representation of Date (-1 if error - test for error by
 *         checking for less than 0.1)
 * @param date
 *          the Date
 */

public static double getExcelDate(final Date date) {
    Calendar calStart = new GregorianCalendar();

    calStart.setTime(date); // If date includes hours, minutes, and seconds, set
                            // them to 0
                            // if (calStart.get(Calendar.YEAR) < 1900)
                            // {
                            // return BAD_DATE;
                            // }
                            // else
                            // {
                            // Because of daylight time saving we cannot use
                            // date.getTime() - calStart.getTimeInMillis()
                            // as the difference in milliseconds between 00:00 and 04:00
                            // can be 3, 4 or 5 hours but Excel expects it to always
                            // be 4 hours.
                            // E.g. 2004-03-28 04:00 CEST - 2004-03-28 00:00 CET is 3 hours
                            // and 2004-10-31 04:00 CET - 2004-10-31 00:00 CEST is 5 hours
    final double fraction = (((calStart.get(Calendar.HOUR_OF_DAY) * 60 + calStart.get(Calendar.MINUTE)) * 60
            + calStart.get(Calendar.SECOND)) * 1000 + calStart.get(Calendar.MILLISECOND))
            / (double) DAY_MILLISECONDS;
    calStart = dayStart(calStart);

    return fraction + (double) absoluteDay(calStart) - CAL_1900_ABSOLUTE;
}

From source file:net.seratch.taskun.util.CalendarUtil.java

 public static Calendar dateTrunc(Calendar calendar) {
   if (calendar == null) {
      return null;
   }/* www.  j a  v  a2  s  .  com*/
   Calendar date = (Calendar) calendar.clone();
   date.set(Calendar.HOUR_OF_DAY, 0);
   date.set(Calendar.MINUTE, 0);
   date.set(Calendar.SECOND, 0);
   date.set(Calendar.MILLISECOND, 0);
   return date;
}

From source file:Dates.java

/**
 * Puts hours, minutes, seconds and milliseconds to zero. <p>
 * //w  w  w  .j a va 2s .  c o m
 * @return The same date sent as argument (a new date is not created). If null
 *      if sent a null is returned.
 */
public static Date removeTime(Date date) {
    if (date == null)
        return null;
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    date.setTime(cal.getTime().getTime());
    return date;
}