Example usage for java.util TimeZone setRawOffset

List of usage examples for java.util TimeZone setRawOffset

Introduction

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

Prototype

public abstract void setRawOffset(int offsetMillis);

Source Link

Document

Sets the base time zone offset to GMT.

Usage

From source file:Main.java

public static void main(String args[]) {

    TimeZone tzone = TimeZone.getDefault();

    // set raw offset
    tzone.setRawOffset(10000);

    // checking offset value
    System.out.println("Offset value is :" + tzone.getRawOffset());
}

From source file:Main.java

/**
 * Converts an XMLGregorianCalendar to a Date.
 *
 * @param xmlDate/*from   w  w w. ja v  a  2 s.  c  o  m*/
 *            XMLGregorianCalendar to convert.
 * @return corresponding date object.
 */
public static Date getDate(final XMLGregorianCalendar xmlDate) {
    // TODO: is this equivalent to getDate(String) processing above??

    // start with UTC, i.e. no daylight savings time.
    TimeZone timezone = TimeZone.getTimeZone("GMT");

    // adjust timezone to match xmldate
    int offsetMinutes = xmlDate.getTimezone();
    if (offsetMinutes != DatatypeConstants.FIELD_UNDEFINED) {
        timezone.setRawOffset(
                // convert minutes to milliseconds
                offsetMinutes * 60 // seconds per minute
                        * 1000 // milliseconds per second
        );
    }

    // use calendar so parsed date will be UTC
    Calendar calendar = Calendar.getInstance(timezone);
    calendar.clear();
    calendar.set(xmlDate.getYear(),
            // xmlcalendar is 1 based, calender is 0 based
            xmlDate.getMonth() - 1, xmlDate.getDay(), xmlDate.getHour(), xmlDate.getMinute(),
            xmlDate.getSecond());
    Date date = calendar.getTime();
    int millis = xmlDate.getMillisecond();
    if (millis != DatatypeConstants.FIELD_UNDEFINED) {
        calendar.setTimeInMillis(calendar.getTimeInMillis() + millis);
    }

    return date;
}

From source file:org.obm.push.protocol.data.TZDecoder.java

public TimeZone decode(String b64) {
    // Doc : [MS-ASDTYPE] 2.7 TimeZone
    // Doc about types :
    // http://msdn.microsoft.com/fr-fr/library/bb469811.aspx
    // 1 LONG = 4 bytes
    // 1 WCHAR = 2 bytes
    // 1 SYSTEMTIME = 8 SHORT = 8 X 2 bytes
    // TOTAL TIMEZONE STRUCT must be 172 bytes

    byte tzstruct[] = Base64.decodeBase64(b64);

    ByteBuffer bfBias = ByteBuffer.wrap(tzstruct, 0, 4);
    // NOT YET USED
    ////from  ww w . jav a  2  s  .  co  m
    // ByteBuffer bfStandardName = ByteBuffer.wrap(tzstruct, 4, 64);
    // ByteBuffer bfStandardDate = ByteBuffer.wrap(tzstruct, 68, 16);
    // ByteBuffer bfStandardBias = ByteBuffer.wrap(tzstruct, 84, 4);
    // ByteBuffer bfDaylightName = ByteBuffer.wrap(tzstruct, 88, 64);
    // ByteBuffer bfDaylightDate = ByteBuffer.wrap(tzstruct, 152, 16);
    // ByteBuffer bfDaylightBias = ByteBuffer.wrap(tzstruct, 168, 4);

    bfBias.order(ByteOrder.LITTLE_ENDIAN);
    int bias = bfBias.getInt(); // Java integer is 4-bytes-long

    // NOT YET USED
    //
    // bfStandardBias.order(ByteOrder.LITTLE_ENDIAN);
    // int standardBias = bfStandardBias.getInt();
    //      
    // bfDaylightBias.order(ByteOrder.LITTLE_ENDIAN);
    // int daylightBias = bfDaylightBias.getInt();

    TimeZone timezone = TimeZone.getDefault();
    timezone.setRawOffset(bias * 60 * 1000);

    String timezones[] = TimeZone.getAvailableIDs(bias * 60 * 1000);
    if (timezones.length > 0) {
        timezone = TimeZone.getTimeZone(timezones[0]);
    }

    // USEFUL DEBUG LINES
    //
    // StringBuffer sb = new StringBuffer();
    // for (int i = 0; i < 172; i+=1) {
    // sb.append(Byte.valueOf(tzstruct[i]).intValue());
    // }
    //      
    // logger.info("b64: " + b64);
    // logger.info("tzstruct: "+ sb.toString());
    // logger.info("bias: " + bias);
    // logger.info("standardbias: " + standardBias);
    // logger.info("standardname: " +
    // bfStandardName.asCharBuffer().toString());
    // logger.info("daylightBias: " + daylightBias);

    return timezone;
}