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

The following examples show how to use java.util.TimeZone#hasSameRules() . 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: WebSession.java    From openmeetings with Apache License 2.0 6 votes vote down vote up
public String getClientTZCode() {
	TimeZone _zone = browserTz;
	if (browserTz == null) {
		try {
			browserTz = getClientInfo().getProperties().getTimeZone();
			if (browserTz != null && !AVAILABLE_TIMEZONE_SET.contains(browserTz.getID())) {
				for (String availableID : AVAILABLE_TIMEZONES) {
					TimeZone zone = TimeZone.getTimeZone(availableID);
					if (zone.hasSameRules(browserTz)) {
						browserTz = zone;
						break;
					}
				}
			}
			_zone = browserTz;
		} catch (Exception e) {
			//no-op
		}
		if (browserTz == null) {
			_zone = Calendar.getInstance(getLocale()).getTimeZone();
		}
	}
	return _zone == null ? null : _zone.getID();
}
 
Example 2
Source File: TimeUtils.java    From biweekly with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static DateTimeValue convert(DateTimeValue time, TimeZone zone, int sense) {
	if (zone == null || zone.hasSameRules(ZULU) || time.year() == 0) {
		return time;
	}

	TimeZone epochTz, dateTimeValueTz;
	if (sense > 0) {
		//time is in UTC; convert to time in zone provided
		epochTz = ZULU;
		dateTimeValueTz = zone;
	} else {
		//time is in local time; convert to UTC
		epochTz = zone;
		dateTimeValueTz = ZULU;
	}

	long epochSeconds = secsSinceEpoch(time);
	long timetMillis = timetMillisFromEpochSecs(epochSeconds, epochTz);
	return toDateTimeValue(timetMillis, dateTimeValueTz);
}
 
Example 3
Source File: JRDataUtils.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Returns a translated date value that has the same fields in a specified
 * timezone as the passed value in the default timezone.
 * 
 * @param value the value to translate
 * @param tz the timezone to translate to
 * @return the date translated to the specified timezone
 */
public static Date translateToTimezone(Date value, TimeZone tz)
{
	if (tz == null)
	{
		return value;
	}
	
	TimeZone defaultTz = TimeZone.getDefault();
	if (defaultTz.hasSameRules(tz))
	{
		// nothing to do
		return value;
	}
	
	long time = value.getTime();
	Date adjustedDate = new Date(time + tz.getOffset(time) - defaultTz.getOffset(time));
	return adjustedDate;
}
 
Example 4
Source File: SimpleDateFormat.java    From Java8CN with Apache License 2.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 5
Source File: SimpleDateFormat.java    From jdk-1.7-annotated with Apache License 2.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 6
Source File: SimpleDateFormat.java    From jdk8u-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 7
Source File: SimpleDateFormat.java    From jdk8u_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 8
Source File: TimeZoneUtilsTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static void assertSameRules(final String label,
                                    final TimeZone expected,
                                    final TimeZone actual) {
  
  if (null == expected && null == actual) return;

  assertNotNull(label + ": expected is null", expected);
  assertNotNull(label + ": actual is null", actual);

  final boolean same = expected.hasSameRules(actual);

  assertTrue(label + ": " + expected.toString() + " [[NOT SAME RULES]] " + 
             actual.toString(),
             same);
}
 
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 hottub 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 11
Source File: TimeUtils.java    From biweekly with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns the timezone with the given name or null if no such timezone.
 * @param tzString the timezone name (e.g. "America/New_York")
 * @return the timezone or null if no such timezone exists
 */
public static TimeZone timeZoneForName(String tzString) {
	TimeZone tz = TimeZone.getTimeZone(tzString);
	if (!tz.hasSameRules(BOGUS_TIMEZONE)) {
		return tz;
	}

	/*
	 * See if the user really was asking for GMT because if
	 * TimeZone.getTimeZone can't recognize tzString, then that is what it
	 * will return.
	 */
	Matcher m = UTC_TZID.matcher(tzString);
	return m.matches() ? TimeUtils.utcTimezone() : null;
}
 
Example 12
Source File: SimpleDateFormat.java    From jdk8u-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 13
Source File: SimpleDateFormat.java    From openjdk-jdk9 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.
 *
 * @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 14
Source File: SimpleDateFormat.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * After reading an object from the input stream, the format
 * pattern in the object is verified.
 *
 * @throws    InvalidObjectException if the pattern is invalid
 */
@java.io.Serial
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: SimpleDateFormat.java    From openjdk-jdk8u-backup 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 16
Source File: SimpleDateFormat.java    From openjdk-jdk8u 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 17
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 18
Source File: SimpleDateFormat.java    From j2objc with Apache License 2.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 19
Source File: SimpleDateFormat.java    From TencentKona-8 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 20
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);
        }
    }
}