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

The following examples show how to use java.util.TimeZone#getDisplayName() . 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: 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 2
Source File: TimeZoneInfo.java    From es6draft with MIT License 5 votes vote down vote up
@Override
public String getDisplayName(TimeZone tz, long date) {
    boolean daylightSavings = tz.inDaylightTime(new Date(date));
    if (!daylightSavings && tz.getOffset(date) != tz.getRawOffset()) {
        daylightSavings = tz.useDaylightTime();
    }
    return tz.getDisplayName(daylightSavings, TimeZone.SHORT, Locale.US);
}
 
Example 3
Source File: elixir1_one_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Gets the time zone display name, using a cache for performance.</p>
 * 
 * @param tz  the zone to query
 * @param daylight  true if daylight savings
 * @param style  the style to use <code>TimeZone.LONG</code>
 *  or <code>TimeZone.SHORT</code>
 * @param locale  the locale to use
 * @return the textual name of the time zone
 */
static synchronized String getTimeZoneDisplay(TimeZone tz, boolean daylight, int style, Locale locale) {
    Object key = new TimeZoneDisplayKey(tz, daylight, style, locale);
    String value = cTimeZoneDisplayCache.get(key);
    if (value == null) {
        // This is a very slow call, so cache the results.
        value = tz.getDisplayName(daylight, style, locale);
        cTimeZoneDisplayCache.put(key, value);
    }
    return value;
}
 
Example 4
Source File: Lang_26_FastDateFormat_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Gets the time zone display name, using a cache for performance.</p>
 * 
 * @param tz  the zone to query
 * @param daylight  true if daylight savings
 * @param style  the style to use <code>TimeZone.LONG</code>
 *  or <code>TimeZone.SHORT</code>
 * @param locale  the locale to use
 * @return the textual name of the time zone
 */
static synchronized String getTimeZoneDisplay(TimeZone tz, boolean daylight, int style, Locale locale) {
    Object key = new TimeZoneDisplayKey(tz, daylight, style, locale);
    String value = cTimeZoneDisplayCache.get(key);
    if (value == null) {
        // This is a very slow call, so cache the results.
        value = tz.getDisplayName(daylight, style, locale);
        cTimeZoneDisplayCache.put(key, value);
    }
    return value;
}
 
Example 5
Source File: Arja_00117_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Gets the time zone display name, using a cache for performance.</p>
 * 
 * @param tz  the zone to query
 * @param daylight  true if daylight savings
 * @param style  the style to use <code>TimeZone.LONG</code>
 *  or <code>TimeZone.SHORT</code>
 * @param locale  the locale to use
 * @return the textual name of the time zone
 */
static synchronized String getTimeZoneDisplay(TimeZone tz, boolean daylight, int style, Locale locale) {
    Object key = new TimeZoneDisplayKey(tz, daylight, style, locale);
    String value = (String) cTimeZoneDisplayCache.get(key);
    if (value == null) {
        // This is a very slow call, so cache the results.
        value = tz.getDisplayName(daylight, style, locale);
        cTimeZoneDisplayCache.put(key, value);
    }
    return value;
}
 
Example 6
Source File: Nopol2017_0089_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 7
Source File: Lang_38_FastDateFormat_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Gets the time zone display name, using a cache for performance.</p>
 * 
 * @param tz  the zone to query
 * @param daylight  true if daylight savings
 * @param style  the style to use <code>TimeZone.LONG</code>
 *  or <code>TimeZone.SHORT</code>
 * @param locale  the locale to use
 * @return the textual name of the time zone
 */
static synchronized String getTimeZoneDisplay(TimeZone tz, boolean daylight, int style, Locale locale) {
    Object key = new TimeZoneDisplayKey(tz, daylight, style, locale);
    String value = cTimeZoneDisplayCache.get(key);
    if (value == null) {
        // This is a very slow call, so cache the results.
        value = tz.getDisplayName(daylight, style, locale);
        cTimeZoneDisplayCache.put(key, value);
    }
    return value;
}
 
Example 8
Source File: FormControllerTests.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
private String getTimezone(int year, int month, int day) {
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.YEAR, year);
	calendar.set(Calendar.MONTH, month);
	calendar.set(Calendar.DAY_OF_MONTH, day);
	Date date = calendar.getTime();
	TimeZone timezone = TimeZone.getDefault();
	boolean inDaylight = timezone.inDaylightTime(date);
	return timezone.getDisplayName(inDaylight, TimeZone.SHORT);
}
 
Example 9
Source File: FastDatePrinter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Gets the time zone display name, using a cache for performance.</p>
 *
 * @param tz  the zone to query
 * @param daylight  true if daylight savings
 * @param style  the style to use {@code TimeZone.LONG} or {@code TimeZone.SHORT}
 * @param locale  the locale to use
 * @return the textual name of the time zone
 */
static String getTimeZoneDisplay(TimeZone tz, boolean daylight, int style, Locale locale) {
    TimeZoneDisplayKey key = new TimeZoneDisplayKey(tz, daylight, style, locale);
    String value = cTimeZoneDisplayCache.get(key);
    if (value == null) {
        // This is a very slow call, so cache the results.
        value = tz.getDisplayName(daylight, style, locale);
        String prior = cTimeZoneDisplayCache.putIfAbsent(key, value);
        if (prior != null) {
            value= prior;
        }
    }
    return value;
}
 
Example 10
Source File: Elixir_008_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Gets the time zone display name, using a cache for performance.</p>
 * 
 * @param tz  the zone to query
 * @param daylight  true if daylight savings
 * @param style  the style to use <code>TimeZone.LONG</code>
 *  or <code>TimeZone.SHORT</code>
 * @param locale  the locale to use
 * @return the textual name of the time zone
 */
static synchronized String getTimeZoneDisplay(TimeZone tz, boolean daylight, int style, Locale locale) {
    Object key = new TimeZoneDisplayKey(tz, daylight, style, locale);
    String value = cTimeZoneDisplayCache.get(key);
    if (value == null) {
        // This is a very slow call, so cache the results.
        value = tz.getDisplayName(daylight, style, locale);
        cTimeZoneDisplayCache.put(key, value);
    }
    return value;
}
 
Example 11
Source File: TimeZoneInfo.java    From es6draft with MIT License 5 votes vote down vote up
@Override
public String getDisplayName(TimeZone tz, long date) {
    DateTimeZone timeZone = toDateTimeZone(tz);
    if (IGNORE_LOCAL_BEFORE_EPOCH) {
        if (date < EPOCH) {
            date = toFirstDateWithNormalizedTime(timeZone, date);
        }
    } else if (IGNORE_LOCAL) {
        if (date <= LAST_LOCAL_TRANSITION) {
            date = toFirstDateWithNormalizedTime(timeZone, date);
        }
    } else if (IGNORE_LMT) {
        if (date <= LAST_LMT_TRANSITION) {
            date = toFirstDateAfterLocalMeanTime(timeZone, date);
        }
    }
    String displayName = timeZone.getNameKey(date);
    if (displayName != null) {
        return displayName;
    }
    boolean daylightSavings = !timeZone.isStandardOffset(date);
    if (!daylightSavings
            && timeZone.getOffset(date) != timeZone.getStandardOffset(System.currentTimeMillis())) {
        daylightSavings = timeZone.nextTransition(date) != date;
    }
    return tz.getDisplayName(daylightSavings, TimeZone.SHORT, Locale.US);
}
 
Example 12
Source File: FastDatePrinter.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Gets the time zone display name, using a cache for performance.</p>
 *
 * @param tz       the zone to query
 * @param daylight true if daylight savings
 * @param style    the style to use {@code TimeZone.LONG} or {@code TimeZone.SHORT}
 * @param locale   the locale to use
 * @return the textual name of the time zone
 */
static String getTimeZoneDisplay(final TimeZone tz, final boolean daylight, final int style, final Locale locale) {
    final TimeZoneDisplayKey key = new TimeZoneDisplayKey(tz, daylight, style, locale);
    String value = cTimeZoneDisplayCache.get(key);
    if (value == null) {
        // This is a very slow call, so cache the results.
        value = tz.getDisplayName(daylight, style, locale);
        final String prior = cTimeZoneDisplayCache.putIfAbsent(key, value);
        if (prior != null) {
            value = prior;
        }
    }
    return value;
}
 
Example 13
Source File: FastDatePrinter.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Gets the time zone display name, using a cache for performance.</p>
 *
 * @param tz       the zone to query
 * @param daylight true if daylight savings
 * @param style    the style to use {@code TimeZone.LONG} or {@code TimeZone.SHORT}
 * @param locale   the locale to use
 * @return the textual name of the time zone
 */
static String getTimeZoneDisplay(final TimeZone tz, final boolean daylight, final int style, final Locale locale) {
    final TimeZoneDisplayKey key = new TimeZoneDisplayKey(tz, daylight, style, locale);
    String value = cTimeZoneDisplayCache.get(key);
    if (value == null) {
        // This is a very slow call, so cache the results.
        value = tz.getDisplayName(daylight, style, locale);
        final String prior = cTimeZoneDisplayCache.putIfAbsent(key, value);
        if (prior != null) {
            value = prior;
        }
    }
    return value;
}
 
Example 14
Source File: HongKong.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void checkTimeZone(String timeZoneID, String expected) {
    TimeZone timeZone = TimeZone.getTimeZone(timeZoneID);
    String actual = timeZone.getDisplayName();
    if (!expected.equals(actual)) {
        throw new RuntimeException();
    }
}
 
Example 15
Source File: TimeZoneNamesTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void testTZName( Locale locale, String timezoneName, boolean isDaylight,
                               int nameType, String expectedName ) throws RuntimeException {
    if (!testGeneric) {
        //Construct time zone objects
        TimeZone zone = TimeZone.getTimeZone(timezoneName);
        //Get name from JDK
        String name = zone.getDisplayName(isDaylight, nameType, locale);
        //Check for equality
        if (!name.equals(expectedName))
            throw new RuntimeException( "Time zone ("+timezoneName+") name is incorrect for locale: \""+locale.getDisplayName()
                                        +"\" nameType:"+requestedTestType+" DST:"+isDaylight+" Should be: " +expectedName+" Observed: "+name);
    }
}
 
Example 16
Source File: TestZoneTextPrinterParser.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void test_printText() {
    Random r = RandomFactory.getRandom();
    int N = 8;
    Locale[] locales = Locale.getAvailableLocales();
    Set<String> zids = ZoneRulesProvider.getAvailableZoneIds();
    ZonedDateTime zdt = ZonedDateTime.now();

    //System.out.printf("locale==%d, timezone=%d%n", locales.length, zids.size());
    while (N-- > 0) {
        zdt = zdt.withDayOfYear(r.nextInt(365) + 1)
                 .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400));
        for (String zid : zids) {
            if (zid.equals("ROC") || zid.startsWith("Etc/GMT")) {
                continue;      // TBD: match jdk behavior?
            }
            zdt = zdt.withZoneSameLocal(ZoneId.of(zid));
            TimeZone tz = TimeZone.getTimeZone(zid);
            boolean isDST = tz.inDaylightTime(new Date(zdt.toInstant().toEpochMilli()));
            for (Locale locale : locales) {
                String longDisplayName = tz.getDisplayName(isDST, TimeZone.LONG, locale);
                String shortDisplayName = tz.getDisplayName(isDST, TimeZone.SHORT, locale);
                if ((longDisplayName.startsWith("GMT+") && shortDisplayName.startsWith("GMT+"))
                        || (longDisplayName.startsWith("GMT-") && shortDisplayName.startsWith("GMT-"))) {
                    printText(locale, zdt, TextStyle.FULL, tz, tz.getID());
                    printText(locale, zdt, TextStyle.SHORT, tz, tz.getID());
                    continue;
                }
                printText(locale, zdt, TextStyle.FULL, tz,
                        tz.getDisplayName(isDST, TimeZone.LONG, locale));
                printText(locale, zdt, TextStyle.SHORT, tz,
                        tz.getDisplayName(isDST, TimeZone.SHORT, locale));
            }
        }
    }
}
 
Example 17
Source File: FastDateFormat.java    From gflogger with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Gets the time zone display name, using a cache for performance.</p>
 * 
 * @param tz  the zone to query
 * @param daylight  true if daylight savings
 * @param style  the style to use <code>TimeZone.LONG</code>
 *  or <code>TimeZone.SHORT</code>
 * @param locale  the locale to use
 * @return the textual name of the time zone
 */
static synchronized String getTimeZoneDisplay(TimeZone tz, boolean daylight, int style, Locale locale) {
	// xxx :: GARBAGE !!!
	Object key = new TimeZoneDisplayKey(tz, daylight, style, locale);
	String value = cTimeZoneDisplayCache.get(key);
	if (value == null) {
		// This is a very slow call, so cache the results.
		value = tz.getDisplayName(daylight, style, locale);
		cTimeZoneDisplayCache.put(key, value);
	}
	return value;
}
 
Example 18
Source File: 1_FastDateFormat.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Gets the time zone display name, using a cache for performance.</p>
 * 
 * @param tz  the zone to query
 * @param daylight  true if daylight savings
 * @param style  the style to use <code>TimeZone.LONG</code>
 *  or <code>TimeZone.SHORT</code>
 * @param locale  the locale to use
 * @return the textual name of the time zone
 */
static synchronized String getTimeZoneDisplay(TimeZone tz, boolean daylight, int style, Locale locale) {
    Object key = new TimeZoneDisplayKey(tz, daylight, style, locale);
    String value = (String) cTimeZoneDisplayCache.get(key);
    if (value == null) {
        // This is a very slow call, so cache the results.
        value = tz.getDisplayName(daylight, style, locale);
        cTimeZoneDisplayCache.put(key, value);
    }
    return value;
}
 
Example 19
Source File: Lang_18_FastDateFormat_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Gets the time zone display name, using a cache for performance.</p>
 *
 * @param tz  the zone to query
 * @param daylight  true if daylight savings
 * @param style  the style to use {@code TimeZone.LONG} or {@code TimeZone.SHORT}
 * @param locale  the locale to use
 * @return the textual name of the time zone
 */
static String getTimeZoneDisplay(TimeZone tz, boolean daylight, int style, Locale locale) {
    TimeZoneDisplayKey key = new TimeZoneDisplayKey(tz, daylight, style, locale);
    String value = cTimeZoneDisplayCache.get(key);
    if (value == null) {
        // This is a very slow call, so cache the results.
        value = tz.getDisplayName(daylight, style, locale);
        String prior = cTimeZoneDisplayCache.putIfAbsent(key, value);
        if (prior != null) {
            value= prior;
        }
    }
    return value;
}
 
Example 20
Source File: BaseNotificationContent.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
/**
 * Get the timezone in String
 */
protected String getTimezoneString(DateTimeZone dateTimeZone) {
  TimeZone tz = TimeZone.getTimeZone(dateTimeZone.getID());
  return tz.getDisplayName(true, 0);
}