Example usage for java.util Calendar ZONE_OFFSET

List of usage examples for java.util Calendar ZONE_OFFSET

Introduction

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

Prototype

int ZONE_OFFSET

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

Click Source Link

Document

Field number for get and set indicating the raw offset from GMT in milliseconds.

Usage

From source file:org.apache.axis2.databinding.utils.ConverterUtil.java

public static String convertToString(Calendar value) {
    if (isCustomClassPresent) {
        return invokeToStringMethod(value, Calendar.class);
    } else {//from   w  w  w  . jav  a2s .c o m
        // lexical form of the calendar is '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?
        if (value.get(Calendar.ZONE_OFFSET) == -1) {
            value.setTimeZone(TimeZone.getDefault());
        }
        StringBuffer dateString = new StringBuffer(28);
        appendDate(dateString, value);
        dateString.append("T");
        //adding hours
        appendTime(value, dateString);
        appendTimeZone(value, dateString);
        return dateString.toString();
    }
}

From source file:ca.uhn.hl7v2.model.primitive.CommonTSTest.java

@Test
public void testNativeJavaAccessorsAndMutators() throws DataTypeException, ParseException {

    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
    Date date = format.parse("20100609 12:40:05");
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);/*from  w w w  . j  a va2s. c  o m*/

    CommonTS commonTS = new CommonTS();
    commonTS.setValueToMinute(cal);
    assertEquals("201006091240", commonTS.getValue());

    commonTS = new CommonTS();
    commonTS.setValueToMinute(date);
    assertEquals("201006091240", commonTS.getValue());

    commonTS = new CommonTS();
    commonTS.setValueToSecond(cal);
    assertEquals("20100609124005", commonTS.getValue());

    commonTS = new CommonTS();
    commonTS.setValueToSecond(date);
    assertEquals("20100609124005", commonTS.getValue());

    commonTS = new CommonTS();
    cal.set(Calendar.MILLISECOND, 250);
    cal.set(Calendar.ZONE_OFFSET, -5 * 1000 * 60 * 60);
    commonTS.setValue(cal);
    String value = commonTS.getValue();
    assertEquals("20100609124005.25-0400", value);

    // 3410095 : proof to avoid overflow
    commonTS = new CommonTS();
    cal.set(Calendar.ZONE_OFFSET, 11 * 1000 * 60 * 60);
    commonTS.setValue(cal);
    value = commonTS.getValue();
    assertEquals("20100609124005.25+1200", value);

    // 3410095-related : proof to cover time zones not at the full hour,
    // e.g. India
    commonTS = new CommonTS();
    cal.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
    commonTS.setValue(cal);
    value = commonTS.getValue();
    assertEquals("20100609061005.25+0530", value);

    commonTS = new CommonTS();
    commonTS.setValue("201006091240");
    assertEquals("201006091240", commonTS.getValue());
    String formatted = format.format(commonTS.getValueAsDate());
    assertEquals("20100609 12:40:00", formatted);

    commonTS = new CommonTS();
    commonTS.setValue("201006091240");
    assertEquals("20100609 12:40:00", format.format(commonTS.getValueAsCalendar().getTime()));

    // Check millis and offset
    commonTS = new CommonTS();
    commonTS.setValue("20100609124005.25-0400");
    cal = commonTS.getValueAsCalendar();
    assertEquals(250, cal.get(Calendar.MILLISECOND));
    assertEquals(-4 * 1000 * 60 * 60, cal.get(Calendar.ZONE_OFFSET));

    // Check DST offset
    commonTS = new CommonTS();
    Date dt = new Date(1364857200035L);
    SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss.SSSZ");
    fmt.setTimeZone(TimeZone.getTimeZone("Canada/Eastern"));
    String dateString = fmt.format(dt);
    commonTS.setValue(dt);
    String tsString = commonTS.getValue();
    assertEquals(dateString, tsString);
}

From source file:org.opencastproject.capture.impl.ConfigurationManager.java

/**
 * Retrieve property for configuration.//from   w  ww .java 2 s.co m
 * 
 * @param key
 *          the key to retrieve from the property list.
 * @return the value corresponding to the key.
 */
public String getItem(String key) {
    if (key == null) {
        return null;
    } else if (StringUtils.equals(key, "capture.device.timezone.offset")) {
        Calendar cal = Calendar.getInstance();
        return Long.toString((cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET))
                / (1 * CaptureParameters.MINUTES * CaptureParameters.MILLISECONDS));
    } else if (StringUtils.equals(key, "capture.device.timezone")) {
        return TimeZone.getDefault().getID();
    } else {
        return properties.getProperty(key);
    }
}

From source file:Currently.java

/**
 * Ignores any internal date format, and tries to show a complete
 * date/timp stamp in extended ISO 8601 format. UTC time (Zulu time)
 * or a local timezone will be used.<p>
 *
 * <table border=1>//from   w  ww.  j ava  2  s .c  om
 * <tr><th>zone</th><th>format</th><th>fraction</td><th>example</th></tr>
 * <tr><td>local</td><td>basic</td><td>integral</td><td>20020523T140427-0500</td></tr>
 * <tr><td>UTC</td><td>basic</td><td>integral</td><td>20020523190427Z</td></tr>
 * <tr><td>local</td><td>extd.</td><td>integral</td><td>2002-05-23T14:04:27-05:00</td></tr>
 * <tr><td>UTC</td><td>extd.</td><td>integral</td><td>2002-05-23T19:04:27Z</td></tr>
 * <tr><td>local</td><td>basic</td><td>millis</td><td>20020523T140427.166-0500</td></tr>
 * <tr><td>UTC</td><td>basic</td><td>millis</td><td>20020523190427.166Z</td></tr>
 * <tr><td>local</td><td>extd.</td><td>millis</td><td>2002-05-23T14:04:27.166-05:00</td></tr>
 * <tr><td>UTC</td><td>extd.</td><td>millis</td><td>2002-05-23T19:04:27.166Z</td></tr>
 * </table><p>
 *
 * @param zuluTime returns a UTC formatted stamp, if true. Otherwise
 * the time will be formatted according to the local zone. Local time
 * should be prefixed with the 'T'.
 * @param extendedFormat will use the extended ISO 8601 format which
 * separates the different timestamp items. If false, the basic
 * format will be used. In UTC and basic format, the 'T' separator
 * will be omitted.
 * @param withMillis will put the millisecond extension into the timestamp.
 * If false, the time will be without millisecond fraction. The separator
 * is taken from {@link java.text.DecimalFormatSymbols#getMinusSign()},
 * which usually is a period or a comma.
 * @param now is a time stamp as Date.
 * @return an ISO 8601 formatted date and time representation for
 * the given point in time.
 */
public static String iso8601(boolean zuluTime, boolean extendedFormat, boolean withMillis, Date now) {
    Calendar c = Calendar.getInstance(zuluTime ? TimeZone.getTimeZone("UTC") : TimeZone.getDefault());
    c.setTime(now);

    // set up formattting options
    DecimalFormat nf2 = new DecimalFormat("##00");
    DecimalFormat nf3 = new DecimalFormat("###000");
    DecimalFormat nf4 = new DecimalFormat("####0000");
    DecimalFormatSymbols dfs = nf2.getDecimalFormatSymbols();

    // allocate result string buffer
    int size = extendedFormat ? (zuluTime ? 25 : 30) : (zuluTime ? 21 : 25);
    if (!withMillis) {
        size -= 4;
    }
    StringBuffer result = new StringBuffer(size);

    result.append(nf4.format(c.get(Calendar.YEAR)));
    if (extendedFormat) {
        result.append('-'); // position 5
    }
    result.append(nf2.format(c.get(Calendar.MONTH) + 1));
    if (extendedFormat) {
        result.append('-'); // position 8
    }
    result.append(nf2.format(c.get(Calendar.DAY_OF_MONTH)));
    // generating UTC in basic format may leave out the 'T' separator
    if (extendedFormat || !zuluTime) {
        result.append('T'); // position 11
    }
    result.append(nf2.format(c.get(Calendar.HOUR_OF_DAY)));
    if (extendedFormat) {
        result.append(':'); // position 14
    }
    result.append(nf2.format(c.get(Calendar.MINUTE)));
    if (extendedFormat) {
        result.append(':'); // position 17
    }
    result.append(nf2.format(c.get(Calendar.SECOND)));

    if (withMillis) {
        // Though there is no explicit spec which allows a complete
        // timestamp with milliseconds, it is implied through two
        // levels, sigh. 5.3.4.2 allows decimal fractions with
        // time-only stamps that have a timezone. The intro of 5.4.2
        // allows 5.3.1.3.
        result.append(dfs.getDecimalSeparator()); // position 20
        result.append(nf3.format(c.get(Calendar.MILLISECOND)));
    }

    if (zuluTime) {
        // this is easy
        result.append('Z');
    } else {
        // time zone calculations
        int zone_offset = c.get(Calendar.ZONE_OFFSET) / 1000;
        int save_offset = c.get(Calendar.DST_OFFSET) / 1000;
        int diff = (zone_offset + save_offset) / 60;
        result.append(diff < 0 ? dfs.getMinusSign() : '+'); // position 24

        if (diff < 0) {
            diff = Math.abs(diff);
        }
        result.append(nf2.format(diff / 60));
        if (extendedFormat) {
            result.append(':');
        }
        result.append(nf2.format(diff % 60));
    }

    return result.toString();
}

From source file:org.opencastproject.capture.impl.ConfigurationManager.java

/**
 * Retrieve property for configuration. The return value for this function do *not* have its variable(s) expanded.
 * /*  ww  w .  j a va2 s  . c o m*/
 * @param key
 *          the key to retrieve from the property list.
 * @return the value corresponding to the key.
 */
public String getUninterpretedItem(String key) {
    if (key == null) {
        return null;
    } else if (StringUtils.equals(key, "capture.device.timezone.offset")) {
        Calendar cal = Calendar.getInstance();
        return Long.toString((cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET))
                / (1 * CaptureParameters.MINUTES * CaptureParameters.MILLISECONDS));
    } else if (StringUtils.equals(key, "capture.device.timezone")) {
        return TimeZone.getDefault().getID();
    } else {
        return properties.getUninterpretedProperty(key);
    }
}

From source file:org.openbravo.service.json.JsonToDataConverter.java

private static Date convertToLocalTime(Date UTCTime) {
    Calendar localTime = Calendar.getInstance();
    localTime.setTime(UTCTime);/*from  w  ww.j  av  a 2  s.com*/

    int gmtMillisecondOffset = (localTime.get(Calendar.ZONE_OFFSET) + localTime.get(Calendar.DST_OFFSET));
    localTime.add(Calendar.MILLISECOND, gmtMillisecondOffset);

    return localTime.getTime();
}

From source file:org.betaconceptframework.astroboa.portal.utility.CalendarUtils.java

public void clearTimeFromCalendar(Calendar calendar) {
    if (calendar != null) {
        calendar.set(Calendar.HOUR, 0);
        calendar.clear(Calendar.AM_PM); //ALWAYS clear AM_PM before HOUR_OF_DAY
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.clear(Calendar.DST_OFFSET);
        calendar.clear(Calendar.ZONE_OFFSET);
    }/*from w  w  w .ja va 2 s  .co m*/

}

From source file:org.exoplatform.contact.CalendarUtils.java

public static Calendar getInstanceTempCalendar() {
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.setLenient(false);/*from  w w w .  jav a  2  s  . co m*/
    int gmtoffset = calendar.get(Calendar.DST_OFFSET) + calendar.get(Calendar.ZONE_OFFSET);
    calendar.setTimeInMillis(System.currentTimeMillis() - gmtoffset);
    return calendar;
}

From source file:com.wavemaker.runtime.server.ServerUtils.java

/**
 * Calculate the server time offset against UTC
 * //from   w w w  . j a v a2s  .c om
 * @return the server time offset in mili-seconds
 */
public static int getServerTimeOffset() {
    Calendar now = Calendar.getInstance();
    return now.get(Calendar.ZONE_OFFSET) + now.get(Calendar.DST_OFFSET);
}

From source file:org.jahia.utils.maven.plugin.contentgenerator.ContentGeneratorService.java

/**
 * Format a date for inclusion in JCR XML file If date is null, current date
 * is used Format used: http://www.day.com/specs/jcr/1.0/6.2.5.1_Date.html
 * //from w  w  w. j a  v  a2s  .c o  m
 * @param date
 * @return formated date
 */
public String getDateForJcrImport(Date date) {
    GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance();
    if (date != null) {
        gc.setTime(date);
    }
    StringBuffer sbNewDate = new StringBuffer();
    // 2011-04-01T17:39:59.265+02:00
    sbNewDate.append(gc.get(Calendar.YEAR));
    sbNewDate.append("-");
    sbNewDate.append(gc.get(Calendar.MONTH));
    sbNewDate.append("-");
    sbNewDate.append(gc.get(Calendar.DAY_OF_MONTH));
    sbNewDate.append("T");
    sbNewDate.append(gc.get(Calendar.HOUR_OF_DAY));
    sbNewDate.append(":");
    sbNewDate.append(gc.get(Calendar.MINUTE));
    sbNewDate.append(":");
    sbNewDate.append(gc.get(Calendar.SECOND));
    sbNewDate.append(".");
    sbNewDate.append(gc.get(Calendar.MILLISECOND));
    sbNewDate.append(gc.get(Calendar.ZONE_OFFSET));
    return sbNewDate.toString();
}