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.augmentum.common.util.DateUtil.java

public static int getDaysBetween(Date startDate, Date endDate, TimeZone timeZone) {

    Calendar startCal = new GregorianCalendar(timeZone);
    startCal.setTime(DateUtils.truncate(startDate, Calendar.DAY_OF_MONTH));

    Calendar endCal = new GregorianCalendar(timeZone);
    endCal.setTime(DateUtils.truncate(endDate, Calendar.DAY_OF_MONTH));

    int daysBetween = 0;
    while (startCal.before(endCal)) {
        daysBetween++;/*from w w w . ja v  a 2  s .c  o m*/

        startCal.add(Calendar.DAY_OF_MONTH, 1);
    }

    return daysBetween;
}

From source file:Main.java

private static Calendar cal(TimeZone tz) {
    Calendar cal = (tz != null) ? new GregorianCalendar(tz) : new GregorianCalendar();
    cal.clear();//w  w  w  .ja v  a2  s.c o m
    return cal;
}

From source file:charva.awt.util.DateEntryField.java

private void init(TimeZone zone_) {
    setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));

    _cal = new GregorianCalendar(zone_);

    add(_yearField);/*from w ww . j a  va  2 s  .c  o m*/
    _yearField.addKeyListener(this);
    _yearField.addFocusListener(this);
    add(new JLabel("/"));
    add(_monthField);
    _monthField.addKeyListener(this);
    _monthField.addFocusListener(this);
    add(new JLabel("/"));
    add(_dayField);
    _dayField.addKeyListener(this);
    _dayField.addFocusListener(this);
}

From source file:Main.java

private static Calendar cal(TimeZone tz, Date date) {
    Calendar cal = (tz != null) ? new GregorianCalendar(tz) : new GregorianCalendar();
    cal.setTime(date);/*from w ww.j  a va2 s .co  m*/
    return cal;
}

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.// w  w  w  . j a v  a2s  . c om
 *
 * @return the {@link Date} .
 */
private static Date getDayStart(final Date d) {
    final Calendar gc = new GregorianCalendar(LOGGING_TIMEZONE);
    gc.setTime(d);
    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

/**
 * 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/*  ww w .  ja v  a2s . 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:com.redhat.rhevm.api.powershell.util.PowerShellUtils.java

public static XMLGregorianCalendar getDate(int secondsAgo) {
    if (secondsAgo == 0) {
        return null;
    }//from  w  w w.  j  a  va  2s .c om
    XMLGregorianCalendar ret = getDatatypeFactory()
            .newXMLGregorianCalendar(new GregorianCalendar(TimeZone.getTimeZone("UTC")));
    ret.add(getDatatypeFactory().newDuration(false, 0, 0, 0, 0, 0, secondsAgo));
    return ret;
}

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

public static Date convertToGMTAbsoluteDate(Date dateValue, String sourceSystemTimezone) {
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone(sourceSystemTimezone));
    cal.setLenient(false);//from  w w w.ja  va2s.c  om
    cal.setTime(dateValue);
    Calendar newCal = new GregorianCalendar(TimeZone.getTimeZone(GMT_TIME_ZONE_STRING));
    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;
}

From source file:name.persistent.behaviours.PURLValidationSupport.java

private XMLGregorianCalendar today() throws DatatypeConfigurationException {
    GregorianCalendar cal;/*w  w  w  .  j  a va2s  .  co  m*/
    XMLGregorianCalendar today;
    int n = DatatypeConstants.FIELD_UNDEFINED;
    DatatypeFactory f = DatatypeFactory.newInstance();
    cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    today = f.newXMLGregorianCalendar(cal);
    today.setTime(n, n, n, n);
    return today;
}

From source file:DateTime.java

/** Formats the value as an RFC 3339 date/time string. */
public String toStringRfc3339() {

    StringBuilder sb = new StringBuilder();

    Calendar dateTime = new GregorianCalendar(GMT);
    long localTime = value;
    Integer tzShift = this.tzShift;
    if (tzShift != null) {
        localTime += tzShift.longValue() * 60000;
    }/*from   ww  w . ja va2  s.  c o  m*/
    dateTime.setTimeInMillis(localTime);

    appendInt(sb, dateTime.get(Calendar.YEAR), 4);
    sb.append('-');
    appendInt(sb, dateTime.get(Calendar.MONTH) + 1, 2);
    sb.append('-');
    appendInt(sb, dateTime.get(Calendar.DAY_OF_MONTH), 2);

    if (!dateOnly) {

        sb.append('T');
        appendInt(sb, dateTime.get(Calendar.HOUR_OF_DAY), 2);
        sb.append(':');
        appendInt(sb, dateTime.get(Calendar.MINUTE), 2);
        sb.append(':');
        appendInt(sb, dateTime.get(Calendar.SECOND), 2);

        if (dateTime.isSet(Calendar.MILLISECOND)) {
            sb.append('.');
            appendInt(sb, dateTime.get(Calendar.MILLISECOND), 3);
        }
    }

    if (tzShift != null) {

        if (tzShift.intValue() == 0) {

            sb.append('Z');

        } else {

            int absTzShift = tzShift.intValue();
            if (tzShift > 0) {
                sb.append('+');
            } else {
                sb.append('-');
                absTzShift = -absTzShift;
            }

            int tzHours = absTzShift / 60;
            int tzMinutes = absTzShift % 60;
            appendInt(sb, tzHours, 2);
            sb.append(':');
            appendInt(sb, tzMinutes, 2);
        }
    }

    return sb.toString();
}