Java Code Examples for java.util.TimeZone#getID()

The following examples show how to use java.util.TimeZone#getID() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimezoneListViewActivity.java    From Travel-Mate with MIT License 6 votes vote down vote up
/**
 * @param tz - timexome to be displayed
 * @return - formatted timezone value
 */
@SuppressLint ("DefaultLocale")
private static String displayTimeZone(TimeZone tz) {
    long hours = TimeUnit.MILLISECONDS.toHours(tz.getRawOffset());
    long minutes = TimeUnit.MILLISECONDS.toMinutes(tz.getRawOffset())
            - TimeUnit.HOURS.toMinutes(hours);
    // avoid -4:-30 issue
    minutes = Math.abs(minutes);

    String result;
    if (hours > 0) {
        result = tz.getID() + " " + String.format("(GMT+%d:%02d)", hours, minutes);
    } else {
        result = tz.getID() + " " + String.format("(GMT%d:%02d)", hours, minutes);
    }

    return result;
}
 
Example 2
Source File: Bug6442006.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {

        TimeZone tz = TimeZone.getTimeZone("Asia/Taipei");
        Locale tzLocale = new Locale("ja");
        String jaStdName = "\u4e2d\u56fd\u6a19\u6e96\u6642";
        String jaDstName = "\u4e2d\u56fd\u590f\u6642\u9593";

        if (!tz.getDisplayName(false, TimeZone.LONG, tzLocale).equals
           (jaStdName))
             throw new RuntimeException("\n" + tzLocale + ": LONG, " +
                                        "non-daylight saving name for " +
                                        tz.getID() +
                                        " should be " +
                                        jaStdName);
        if (!tz.getDisplayName(true, TimeZone.LONG, tzLocale).equals
           (jaDstName))
             throw new RuntimeException("\n" + tzLocale + ": LONG, " +
                                        "daylight saving name for " +
                                        tz.getID() +
                                        " should be " +
                                        jaDstName);
    }
 
Example 3
Source File: Time_19_DateTimeZone_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Gets a time zone instance for a JDK TimeZone.
 * <p>
 * DateTimeZone only accepts a subset of the IDs from TimeZone. The
 * excluded IDs are the short three letter form (except UTC). This 
 * method will attempt to convert between time zones created using the
 * short IDs and the full version.
 * <p>
 * This method is not designed to parse time zones with rules created by
 * applications using <code>SimpleTimeZone</code> directly.
 * 
 * @param zone  the zone to convert, null means default
 * @return the DateTimeZone object for the zone
 * @throws IllegalArgumentException if the zone is not recognised
 */
public static DateTimeZone forTimeZone(TimeZone zone) {
    if (zone == null) {
        return getDefault();
    }
    final String id = zone.getID();
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }

    // Convert from old alias before consulting provider since they may differ.
    DateTimeZone dtz = null;
    String convId = getConvertedId(id);
    if (convId != null) {
        dtz = cProvider.getZone(convId);
    }
    if (dtz == null) {
        dtz = cProvider.getZone(id);
    }
    if (dtz != null) {
        return dtz;
    }

    // Support GMT+/-hh:mm formats
    if (convId == null) {
        convId = zone.getDisplayName();
        if (convId.startsWith("GMT+") || convId.startsWith("GMT-")) {
            convId = convId.substring(3);
            int offset = parseOffset(convId);
            if (offset == 0L) {
                return DateTimeZone.UTC;
            } else {
                convId = printOffset(offset);
                return fixedOffsetZone(convId, offset);
            }
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example 4
Source File: ProtocolOptions.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public ProtocolOptions ( final int timeout1, final int timeout2, final int timeout3, final ASDUAddressType adsuAddressType, final InformationObjectAddressType informationObjectAddressType, final CauseOfTransmissionType causeOfTransmissionType, final short maxUnacknowledged, final short acknowledgeWindow, final TimeZone timeZone, final boolean ignoreDaylightSavingTime )
{
    super ();
    this.timeout1 = timeout1;
    this.timeout2 = timeout2;
    this.timeout3 = timeout3;
    this.adsuAddressType = adsuAddressType;
    this.informationObjectAddressType = informationObjectAddressType;
    this.causeOfTransmissionType = causeOfTransmissionType;
    this.maxUnacknowledged = maxUnacknowledged;
    this.acknowledgeWindow = acknowledgeWindow;
    this.timeZone = timeZone;
    this.timeZoneId = timeZone != null ? timeZone.getID () : null;
    this.ignoreDaylightSavingTime = ignoreDaylightSavingTime;
}
 
Example 5
Source File: Time_9_DateTimeZone_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Gets a time zone instance for a JDK TimeZone.
 * <p>
 * DateTimeZone only accepts a subset of the IDs from TimeZone. The
 * excluded IDs are the short three letter form (except UTC). This 
 * method will attempt to convert between time zones created using the
 * short IDs and the full version.
 * <p>
 * This method is not designed to parse time zones with rules created by
 * applications using <code>SimpleTimeZone</code> directly.
 * 
 * @param zone  the zone to convert, null means default
 * @return the DateTimeZone object for the zone
 * @throws IllegalArgumentException if the zone is not recognised
 */
public static DateTimeZone forTimeZone(TimeZone zone) {
    if (zone == null) {
        return getDefault();
    }
    final String id = zone.getID();
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }

    // Convert from old alias before consulting provider since they may differ.
    DateTimeZone dtz = null;
    String convId = getConvertedId(id);
    if (convId != null) {
        dtz = cProvider.getZone(convId);
    }
    if (dtz == null) {
        dtz = cProvider.getZone(id);
    }
    if (dtz != null) {
        return dtz;
    }

    // Support GMT+/-hh:mm formats
    if (convId == null) {
        convId = zone.getID();
        if (convId.startsWith("GMT+") || convId.startsWith("GMT-")) {
            convId = convId.substring(3);
            int offset = parseOffset(convId);
            if (offset == 0L) {
                return DateTimeZone.UTC;
            } else {
                convId = printOffset(offset);
                return fixedOffsetZone(convId, offset);
            }
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example 6
Source File: TestXMLGregorianCalendarTimeZone.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void test(int offsetMinutes)
        throws DatatypeConfigurationException {
    XMLGregorianCalendar calendar = DatatypeFactory.newInstance().
            newXMLGregorianCalendar();
    calendar.setTimezone(60 + offsetMinutes);
    TimeZone timeZone = calendar.getTimeZone(DatatypeConstants.FIELD_UNDEFINED);
    String expected = (offsetMinutes < 10 ? "GMT+01:0" : "GMT+01:")
            + offsetMinutes;
    if (!timeZone.getID().equals(expected)) {
        throw new RuntimeException("Test failed: expected timezone: " +
                expected + " Actual: " + timeZone.getID());
    }
}
 
Example 7
Source File: TestXMLGregorianCalendarTimeZone.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void test(int offsetMinutes)
        throws DatatypeConfigurationException {
    XMLGregorianCalendar calendar = DatatypeFactory.newInstance().
            newXMLGregorianCalendar();
    calendar.setTimezone(60 + offsetMinutes);
    TimeZone timeZone = calendar.getTimeZone(DatatypeConstants.FIELD_UNDEFINED);
    String expected = (offsetMinutes < 10 ? "GMT+01:0" : "GMT+01:")
            + offsetMinutes;
    if (!timeZone.getID().equals(expected)) {
        throw new RuntimeException("Test failed: expected timezone: " +
                expected + " Actual: " + timeZone.getID());
    }
}
 
Example 8
Source File: SimpleDateFormat.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * After reading an object from the input stream, the format
 * pattern in the object is verified.
 * <p>
 * @exception InvalidObjectException if the pattern is invalid
 */
private void readObject(ObjectInputStream stream)
                     throws IOException, ClassNotFoundException {
    stream.defaultReadObject();

    try {
        compiledPattern = compile(pattern);
    } catch (Exception e) {
        throw new InvalidObjectException("invalid pattern");
    }

    if (serialVersionOnStream < 1) {
        // didn't have defaultCenturyStart field
        initializeDefaultCentury();
    }
    else {
        // fill in dependent transient field
        parseAmbiguousDatesAsAfter(defaultCenturyStart);
    }
    serialVersionOnStream = currentSerialVersion;

    // If the deserialized object has a SimpleTimeZone, try
    // to replace it with a ZoneInfo equivalent in order to
    // be compatible with the SimpleTimeZone-based
    // implementation as much as possible.
    TimeZone tz = getTimeZone();
    if (tz instanceof SimpleTimeZone) {
        String id = tz.getID();
        TimeZone zi = TimeZone.getTimeZone(id);
        if (zi != null && zi.hasSameRules(tz) && zi.getID().equals(id)) {
            setTimeZone(zi);
        }
    }
}
 
Example 9
Source File: SimpleDateFormat.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * After reading an object from the input stream, the format
 * pattern in the object is verified.
 * <p>
 * @exception InvalidObjectException if the pattern is invalid
 */
private void readObject(ObjectInputStream stream)
                     throws IOException, ClassNotFoundException {
    stream.defaultReadObject();

    try {
        compiledPattern = compile(pattern);
    } catch (Exception e) {
        throw new InvalidObjectException("invalid pattern");
    }

    if (serialVersionOnStream < 1) {
        // didn't have defaultCenturyStart field
        initializeDefaultCentury();
    }
    else {
        // fill in dependent transient field
        parseAmbiguousDatesAsAfter(defaultCenturyStart);
    }
    serialVersionOnStream = currentSerialVersion;

    // If the deserialized object has a SimpleTimeZone, try
    // to replace it with a ZoneInfo equivalent in order to
    // be compatible with the SimpleTimeZone-based
    // implementation as much as possible.
    TimeZone tz = getTimeZone();
    if (tz instanceof SimpleTimeZone) {
        String id = tz.getID();
        TimeZone zi = TimeZone.getTimeZone(id);
        if (zi != null && zi.hasSameRules(tz) && zi.getID().equals(id)) {
            setTimeZone(zi);
        }
    }
}
 
Example 10
Source File: SimpleDateFormat.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * After reading an object from the input stream, the format
 * pattern in the object is verified.
 * <p>
 * @exception InvalidObjectException if the pattern is invalid
 */
private void readObject(ObjectInputStream stream)
                     throws IOException, ClassNotFoundException {
    stream.defaultReadObject();

    try {
        compiledPattern = compile(pattern);
    } catch (Exception e) {
        throw new InvalidObjectException("invalid pattern");
    }

    if (serialVersionOnStream < 1) {
        // didn't have defaultCenturyStart field
        initializeDefaultCentury();
    }
    else {
        // fill in dependent transient field
        parseAmbiguousDatesAsAfter(defaultCenturyStart);
    }
    serialVersionOnStream = currentSerialVersion;

    // If the deserialized object has a SimpleTimeZone, try
    // to replace it with a ZoneInfo equivalent in order to
    // be compatible with the SimpleTimeZone-based
    // implementation as much as possible.
    TimeZone tz = getTimeZone();
    if (tz instanceof SimpleTimeZone) {
        String id = tz.getID();
        TimeZone zi = TimeZone.getTimeZone(id);
        if (zi != null && zi.hasSameRules(tz) && zi.getID().equals(id)) {
            setTimeZone(zi);
        }
    }
}
 
Example 11
Source File: Cardumen_00189_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Gets a time zone instance for a JDK TimeZone.
 * <p>
 * DateTimeZone only accepts a subset of the IDs from TimeZone. The
 * excluded IDs are the short three letter form (except UTC). This 
 * method will attempt to convert between time zones created using the
 * short IDs and the full version.
 * <p>
 * This method is not designed to parse time zones with rules created by
 * applications using <code>SimpleTimeZone</code> directly.
 * 
 * @param zone  the zone to convert, null means default
 * @return the DateTimeZone object for the zone
 * @throws IllegalArgumentException if the zone is not recognised
 */
public static DateTimeZone forTimeZone(TimeZone zone) {
    if (zone == null) {
        return getDefault();
    }
    final String id = zone.getID();
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }

    // Convert from old alias before consulting provider since they may differ.
    DateTimeZone dtz = null;
    String convId = getConvertedId(id);
    if (convId != null) {
        dtz = cProvider.getZone(convId);
    }
    if (dtz == null) {
        dtz = cProvider.getZone(id);
    }
    if (dtz != null) {
        return dtz;
    }

    // Support GMT+/-hh:mm formats
    if (convId == null) {
        convId = zone.getDisplayName();
        if (convId.startsWith("GMT+") || convId.startsWith("GMT-")) {
            convId = convId.substring(3);
            int offset = parseOffset(convId);
            if (offset == 0L) {
                return DateTimeZone.UTC;
            } else {
                convId = printOffset(offset);
                return fixedOffsetZone(convId, offset);
            }
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example 12
Source File: DateTimeZone.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets a time zone instance for a JDK TimeZone.
 * <p>
 * DateTimeZone only accepts a subset of the IDs from TimeZone. The
 * excluded IDs are the short three letter form (except UTC). This 
 * method will attempt to convert between time zones created using the
 * short IDs and the full version.
 * <p>
 * This method is not designed to parse time zones with rules created by
 * applications using <code>SimpleTimeZone</code> directly.
 * 
 * @param zone  the zone to convert, null means default
 * @return the DateTimeZone object for the zone
 * @throws IllegalArgumentException if the zone is not recognised
 */
public static DateTimeZone forTimeZone(TimeZone zone) {
    if (zone == null) {
        return getDefault();
    }
    final String id = zone.getID();
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }

    // Convert from old alias before consulting provider since they may differ.
    DateTimeZone dtz = null;
    String convId = getConvertedId(id);
    if (convId != null) {
        dtz = cProvider.getZone(convId);
    }
    if (dtz == null) {
        dtz = cProvider.getZone(id);
    }
    if (dtz != null) {
        return dtz;
    }

    // Support GMT+/-hh:mm formats
    if (convId == null) {
        convId = zone.getID();
        if (convId.startsWith("GMT+") || convId.startsWith("GMT-")) {
            convId = convId.substring(3);
            int offset = parseOffset(convId);
            if (offset == 0L) {
                return DateTimeZone.UTC;
            } else {
                convId = printOffset(offset);
                return fixedOffsetZone(convId, offset);
            }
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example 13
Source File: TimeUtils.java    From Doradus with Apache License 2.0 5 votes vote down vote up
public static TimeZone getTimeZone(String timeZone) {
    timeZone = timeZone.trim();
    TimeZone zone = TimeZone.getTimeZone(timeZone);
    String id = zone.getID();
    if (id.compareTo(timeZone) != 0) {
        throw new IllegalArgumentException("Bad timezone name: '" + timeZone + "'");
    }
    return zone;
}
 
Example 14
Source File: SimpleDateFormat.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * After reading an object from the input stream, the format
 * pattern in the object is verified.
 * <p>
 * @exception InvalidObjectException if the pattern is invalid
 */
private void readObject(ObjectInputStream stream)
                     throws IOException, ClassNotFoundException {
    stream.defaultReadObject();

    try {
        compiledPattern = compile(pattern);
    } catch (Exception e) {
        throw new InvalidObjectException("invalid pattern");
    }

    if (serialVersionOnStream < 1) {
        // didn't have defaultCenturyStart field
        initializeDefaultCentury();
    }
    else {
        // fill in dependent transient field
        parseAmbiguousDatesAsAfter(defaultCenturyStart);
    }
    serialVersionOnStream = currentSerialVersion;

    // If the deserialized object has a SimpleTimeZone, try
    // to replace it with a ZoneInfo equivalent in order to
    // be compatible with the SimpleTimeZone-based
    // implementation as much as possible.
    TimeZone tz = getTimeZone();
    if (tz instanceof SimpleTimeZone) {
        String id = tz.getID();
        TimeZone zi = TimeZone.getTimeZone(id);
        if (zi != null && zi.hasSameRules(tz) && zi.getID().equals(id)) {
            setTimeZone(zi);
        }
    }
}
 
Example 15
Source File: TimeWithTimeZoneString.java    From Bats with Apache License 2.0 5 votes vote down vote up
public TimeWithTimeZoneString withTimeZone(TimeZone timeZone) {
  if (this.timeZone.equals(timeZone)) {
    return this;
  }
  String localTimeString = localTime.toString();
  String v;
  String fraction;
  int i = localTimeString.indexOf('.');
  if (i >= 0) {
    v = localTimeString.substring(0, i);
    fraction = localTimeString.substring(i + 1);
  } else {
    v = localTimeString;
    fraction = null;
  }
  final DateTimeUtils.PrecisionTime pt =
      DateTimeUtils.parsePrecisionDateTimeLiteral(v,
          new SimpleDateFormat(DateTimeUtils.TIME_FORMAT_STRING, Locale.ROOT),
          this.timeZone, -1);
  pt.getCalendar().setTimeZone(timeZone);
  if (fraction != null) {
    return new TimeWithTimeZoneString(
        pt.getCalendar().get(Calendar.HOUR_OF_DAY),
        pt.getCalendar().get(Calendar.MINUTE),
        pt.getCalendar().get(Calendar.SECOND),
        timeZone.getID())
            .withFraction(fraction);
  }
  return new TimeWithTimeZoneString(
      pt.getCalendar().get(Calendar.HOUR_OF_DAY),
      pt.getCalendar().get(Calendar.MINUTE),
      pt.getCalendar().get(Calendar.SECOND),
      timeZone.getID());
}
 
Example 16
Source File: Device.java    From app-icon with MIT License 4 votes vote down vote up
public String getTimeZoneID() {
    TimeZone tz = TimeZone.getDefault();
    return (tz.getID());
}
 
Example 17
Source File: DateTimeHelper.java    From incubator-atlas with Apache License 2.0 4 votes vote down vote up
public static String getTimeZoneId(TimeZone tz) {
    return tz.getID();
}
 
Example 18
Source File: TimeZoneStringBinding.java    From jadira with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String marshal(TimeZone object) {
    return object.getID();
}
 
Example 19
Source File: TimeZoneConversion.java    From gradle-avro-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public CharSequence toCharSequence(TimeZone value, Schema schema, LogicalType type) {
    return value.getID();
}
 
Example 20
Source File: ICalWriterTest.java    From biweekly with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Test
public void setGlobalTimezone() throws Throwable {
	ICalendar ical = new ICalendar();
	ical.getProperties().clear();

	VEvent event = new VEvent();
	event.getProperties().clear();
	event.setDateStart(utc("1996-07-04 12:00:00"));
	ical.addEvent(event);

	TimeZone nyTimezone = TimeZone.getTimeZone("America/New_York");
	VTimezone nyComponent = new VTimezone(nyTimezone.getID());
	ical.getTimezoneInfo().setDefaultTimezone(new TimezoneAssignment(nyTimezone, nyComponent));

	TimeZone laTimezone = TimeZone.getTimeZone("America/Los_Angeles");
	VTimezone laComponent = new VTimezone(laTimezone.getID());

	StringWriter sw = new StringWriter();
	ICalWriter writer = new ICalWriter(sw, V2_0);
	writer.write(ical);
	writer.setGlobalTimezone(new TimezoneAssignment(laTimezone, laComponent));
	writer.write(ical);
	writer.close();

	//@formatter:off
	String expected =
	"BEGIN:VCALENDAR\r\n" +
		"VERSION:2.0\r\n" +
		"BEGIN:VTIMEZONE\r\n" +
			"TZID:America/New_York\r\n" +
		"END:VTIMEZONE\r\n" +
		"BEGIN:VEVENT\r\n" +
			"DTSTART;TZID=America/New_York:19960704T080000\r\n" +
		"END:VEVENT\r\n" +
	"END:VCALENDAR\r\n" +
	"BEGIN:VCALENDAR\r\n" +
		"VERSION:2.0\r\n" +
		"BEGIN:VTIMEZONE\r\n" +
			"TZID:America/Los_Angeles\r\n" +
		"END:VTIMEZONE\r\n" +
		"BEGIN:VEVENT\r\n" +
			"DTSTART;TZID=America/Los_Angeles:19960704T050000\r\n" +
		"END:VEVENT\r\n" +
	"END:VCALENDAR\r\n";
	//@formatter:on

	String actual = sw.toString();
	assertEquals(expected, actual);
}