Example usage for java.util GregorianCalendar GregorianCalendar

List of usage examples for java.util GregorianCalendar GregorianCalendar

Introduction

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

Prototype

public GregorianCalendar(Locale aLocale) 

Source Link

Document

Constructs a GregorianCalendar based on the current time in the default time zone with the given locale.

Usage

From source file:com.collabnet.ccf.core.utils.DateUtil.java

/**
 * Converts the given Date object into another date object with the
 * specified time zone information.//from w w w . j a v a  2 s.  c  om
 * 
 * @param date
 *            - The date that is to be converted to a different time zone.
 * @param toTimeZone
 *            - the time zone to which the date object should be converted.
 * @return the new Date object in the specified time zone.
 * @throws ParseException
 */
public static Date convertDate(Date date, String toTimeZone) throws ParseException {
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone(toTimeZone));
    cal.setTime(date);
    DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss.SSS Z");
    df.setCalendar(cal);
    return df.parse(df.format(cal.getTime()));
}

From source file:com.qpark.eip.core.spring.statistics.impl.SysUserStatisticsChannelInvocationListener.java

/**
 * Get a {@link Date}, where hours, minutes, seconds and milliseconds are
 * set to 0./*from  www .  j  a  v a  2 s .  c  o  m*/
 *
 * @return the {@link Date} and the corresponding log string.
 */
public static Calendar getRequestDate() {
    Calendar gc = new GregorianCalendar(LOGGING_TIMEZONE);
    gc.set(Calendar.HOUR_OF_DAY, 0);
    gc.set(Calendar.MINUTE, 0);
    gc.set(Calendar.SECOND, 0);
    gc.set(Calendar.MILLISECOND, 0);
    return gc;
}

From source file:Main.java

/**
 * Parses an xs:dateTime attribute value, returning the parsed timestamp in milliseconds since
 * the epoch./*from w  ww .j ava 2  s .  c  o m*/
 *
 * @param value The attribute value to parse.
 * @return The parsed timestamp in milliseconds since the epoch.
 */
public static long parseXsDateTime(String value) throws ParseException {
    Matcher matcher = XS_DATE_TIME_PATTERN.matcher(value);
    if (!matcher.matches()) {
        throw new ParseException("Invalid date/time format: " + value, 0);
    }

    int timezoneShift;
    if (matcher.group(9) == null) {
        // No time zone specified.
        timezoneShift = 0;
    } else if (matcher.group(9).equalsIgnoreCase("Z")) {
        timezoneShift = 0;
    } else {
        timezoneShift = ((Integer.parseInt(matcher.group(12)) * 60 + Integer.parseInt(matcher.group(13))));
        if (matcher.group(11).equals("-")) {
            timezoneShift *= -1;
        }
    }

    Calendar dateTime = new GregorianCalendar(TimeZone.getTimeZone("GMT"));

    dateTime.clear();
    // Note: The month value is 0-based, hence the -1 on group(2)
    dateTime.set(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1,
            Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)),
            Integer.parseInt(matcher.group(5)), Integer.parseInt(matcher.group(6)));
    if (!TextUtils.isEmpty(matcher.group(8))) {
        final BigDecimal bd = new BigDecimal("0." + matcher.group(8));
        // we care only for milliseconds, so movePointRight(3)
        dateTime.set(Calendar.MILLISECOND, bd.movePointRight(3).intValue());
    }

    long time = dateTime.getTimeInMillis();
    if (timezoneShift != 0) {
        time -= timezoneShift * 60000;
    }

    return time;
}

From source file:com.collabnet.ccf.core.utils.DateUtil.java

public static Date convertDateToTimeZone(Date date, String toTimeZone) {
    Calendar toCal = new GregorianCalendar(TimeZone.getTimeZone(toTimeZone));
    Calendar fromCal = new GregorianCalendar();
    fromCal.setTime(date);//from w ww.  j a  va 2 s .  c  o  m

    toCal.set(fromCal.get(Calendar.YEAR), fromCal.get(Calendar.MONTH), fromCal.get(Calendar.DAY_OF_MONTH),
            fromCal.get(Calendar.HOUR_OF_DAY), fromCal.get(Calendar.MINUTE), fromCal.get(Calendar.SECOND));
    toCal.set(Calendar.MILLISECOND, fromCal.get(Calendar.MILLISECOND));

    return toCal.getTime();
}

From source file:com.ewcms.common.query.mongo.EasyQueryInit.java

public EasyQueryInit(MongoOperations operations) {
    this.operations = operations;
    format = new SimpleDateFormat("yyyy-MM-dd");
    format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));

}

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  www .  ja  v a  2  s .c  om
 * @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
        int milliseconds = 0; // always use 0 otherwise returned date will include millis of current time
        if (date.charAt(offset) == '.') {
            checkOffset(date, offset, '.');
            milliseconds = parseInt(date, offset += 1, offset += 3);
        }

        // extract timezone
        String timezoneId;
        char timezoneIndicator = date.charAt(offset);
        if (timezoneIndicator == '+' || timezoneIndicator == '-') {
            timezoneId = GMT_ID + date.substring(offset);
        } else if (timezoneIndicator == 'Z') {
            timezoneId = GMT_ID;
        } else {
            throw new IndexOutOfBoundsException("Invalid time zone indicator " + timezoneIndicator);
        }
        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 e) {
        throw new IllegalArgumentException("Failed to parse date " + date, e);
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException("Failed to parse date " + date, e);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("Failed to parse date " + date, e);
    }
}

From source file:com.nexmo.client.voice.CallsFilterTest.java

@Test
public void testAllParams() {
    /*/*from  w  w  w  . j  a  v a 2 s .co  m*/
    private Integer recordIndex;
    private String order;
     */

    Calendar startCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    startCalendar.set(2016, Calendar.JANUARY, 1, 7, 8, 20);
    startCalendar.set(Calendar.MILLISECOND, 0);
    Date startDate = startCalendar.getTime();
    Calendar endCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    endCalendar.set(2016, Calendar.JANUARY, 1, 7, 8, 55);
    endCalendar.set(Calendar.MILLISECOND, 0);
    Date endDate = endCalendar.getTime();

    CallsFilter filter = new CallsFilter();
    filter.setStatus(CallStatus.COMPLETED);
    filter.setDateStart(startDate);
    filter.setDateEnd(endDate);
    filter.setRecordIndex(12);
    filter.setOrder("asc");
    filter.setPageSize(10);
    filter.setConversationUuid("this-is-not-a-uuid");

    assertEquals(CallStatus.COMPLETED, filter.getStatus());
    assertEquals(startDate, filter.getDateStart());
    assertEquals(endDate, filter.getDateEnd());
    assertEquals(12, (int) filter.getRecordIndex());
    assertEquals("asc", filter.getOrder());
    assertEquals(10, (int) filter.getPageSize());
    assertEquals("this-is-not-a-uuid", filter.getConversationUuid());

    List<NameValuePair> params = filter.toUrlParams();
    Map<String, String> paramLookup = new HashMap<String, String>();
    for (NameValuePair pair : params) {
        paramLookup.put(pair.getName(), pair.getValue());
    }

    assertEquals("completed", paramLookup.get("status"));
    assertEquals("2016-01-01T07:08:55Z", paramLookup.get("date_end"));
    assertEquals("2016-01-01T07:08:20Z", paramLookup.get("date_start"));
    assertEquals("12", paramLookup.get("record_index"));
    assertEquals("asc", paramLookup.get("order"));
    assertEquals("10", paramLookup.get("page_size"));
    assertEquals("this-is-not-a-uuid", paramLookup.get("conversation_uuid"));
}

From source file:edu.fullerton.framemonitor.ChangeListener.java

public ChangeListener(PrintStream log, BlockingQueue<File> outQue) {
    this.log = log;
    this.outQue = outQue;

    sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleTimeZone utctz = new SimpleTimeZone(0, "UTC");
    GregorianCalendar now = new GregorianCalendar(utctz);
    sdf.setTimeZone(utctz);//from w ww. j a  va 2s  . c  o  m
    fnamePat = Pattern.compile("(.+)-(\\d+)-(\\d+)\\..*(gwf|tmp)");
}

From source file:com.qpark.eip.core.spring.statistics.dao.StatisticsLoggingDao.java

/**
 * Get a {@link Date}, where hours, minutes, seconds and milliseconds are
 * set to 0.//from w w  w .  j  a v a 2s.  c o m
 *
 * @return the {@link Date} .
 */
private static Date getDayEnd(final Date d) {
    final Calendar gc = new GregorianCalendar(LOGGING_TIMEZONE);
    gc.setTime(d);
    gc.set(Calendar.HOUR_OF_DAY, 23);
    gc.set(Calendar.MINUTE, 59);
    gc.set(Calendar.SECOND, 59);
    gc.set(Calendar.MILLISECOND, 0);
    return gc.getTime();
}

From source file:com.collabnet.ccf.core.utils.DateUtil.java

public static Date convertGMTToTimezoneAbsoluteDate(Date dateValue, String sourceSystemTimezone) {
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone(GMT_TIME_ZONE_STRING));
    cal.setLenient(false);/* w w w  .j a  v  a2 s. c o m*/
    cal.setTime(dateValue);
    Calendar newCal = new GregorianCalendar(TimeZone.getTimeZone(sourceSystemTimezone));
    newCal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
    newCal.set(Calendar.MILLISECOND, 0);
    Date returnDate = newCal.getTime();
    return returnDate;
}