/** * Return the TimeZone associated with the current thread, if any, * or the system default TimeZone otherwise. This is effectively a * replacement for {@link java.util.TimeZone#getDefault()}, * able to optionally respect a user-level TimeZone setting. * <p>Note: This method has a fallback to the shared default TimeZone, * either at the framework level or at the JVM-wide system level. * If you'd like to check for the raw LocaleContext content * (which may indicate no specific time zone through {@code null}, use * {@link #getLocaleContext()} and call {@link TimeZoneAwareLocaleContext#getTimeZone()} * after downcasting to {@link TimeZoneAwareLocaleContext}. * @return the current TimeZone, or the system default TimeZone if no * specific TimeZone has been associated with the current thread * @see #getLocaleContext() * @see TimeZoneAwareLocaleContext#getTimeZone() * @see #setDefaultTimeZone(TimeZone) * @see java.util.TimeZone#getDefault() */ public static TimeZone getTimeZone() { return getTimeZone(getLocaleContext()); }
/** * Return the TimeZone associated with the current thread, if any, * or the system default TimeZone otherwise. This is effectively a * replacement for {@link java.util.TimeZone#getDefault()}, * able to optionally respect a user-level TimeZone setting. * <p>Note: This method has a fallback to the shared default TimeZone, * either at the framework level or at the JVM-wide system level. * If you'd like to check for the raw LocaleContext content * (which may indicate no specific time zone through {@code null}, use * {@link #getLocaleContext()} and call {@link TimeZoneAwareLocaleContext#getTimeZone()} * after downcasting to {@link TimeZoneAwareLocaleContext}. * @return the current TimeZone, or the system default TimeZone if no * specific TimeZone has been associated with the current thread * @see #getLocaleContext() * @see TimeZoneAwareLocaleContext#getTimeZone() * @see #setDefaultTimeZone(TimeZone) * @see java.util.TimeZone#getDefault() */ public static TimeZone getTimeZone() { return getTimeZone(getLocaleContext()); }
@Test public void testSetTimeZoneAwareLocaleContext() { LocaleContext lc = new SimpleTimeZoneAwareLocaleContext(Locale.GERMANY, TimeZone.getTimeZone("GMT+1")); LocaleContextHolder.setLocaleContext(lc); assertSame(lc, LocaleContextHolder.getLocaleContext()); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); LocaleContextHolder.resetLocaleContext(); assertNull(LocaleContextHolder.getLocaleContext()); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); }
throw new ServletException("Incorrect TimeZone"); if (!TimeZone.getDefault().equals(LocaleContextHolder.getTimeZone())) { throw new ServletException("Incorrect TimeZone"); throw new ServletException("Incorrect TimeZone"); if (!TimeZone.getTimeZone("GMT+1").equals(LocaleContextHolder.getTimeZone())) { throw new ServletException("Incorrect TimeZone");
@Test public void testSetLocaleContext() { LocaleContext lc = new SimpleLocaleContext(Locale.GERMAN); LocaleContextHolder.setLocaleContext(lc); assertSame(lc, LocaleContextHolder.getLocaleContext()); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); lc = new SimpleLocaleContext(Locale.GERMANY); LocaleContextHolder.setLocaleContext(lc); assertSame(lc, LocaleContextHolder.getLocaleContext()); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); LocaleContextHolder.resetLocaleContext(); assertNull(LocaleContextHolder.getLocaleContext()); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); }
@Test public void testSetTimeZone() { LocaleContextHolder.setTimeZone(TimeZone.getTimeZone("GMT+1")); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertNull(LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); LocaleContextHolder.setTimeZone(TimeZone.getTimeZone("GMT+2")); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getTimeZone("GMT+2"), LocaleContextHolder.getTimeZone()); assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertNull(LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getTimeZone("GMT+2"), ((TimeZoneAwareLocaleContext) LocaleContextHolder.getLocaleContext()).getTimeZone()); LocaleContextHolder.setTimeZone(null); assertNull(LocaleContextHolder.getLocaleContext()); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); LocaleContextHolder.setDefaultTimeZone(TimeZone.getTimeZone("GMT+1")); assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); LocaleContextHolder.setDefaultTimeZone(null); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); }
@Test public void testSetLocale() { LocaleContextHolder.setLocale(Locale.GERMAN); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); assertFalse(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocaleContext().getLocale()); LocaleContextHolder.setLocale(Locale.GERMANY); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); assertFalse(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocaleContext().getLocale()); LocaleContextHolder.setLocale(null); assertNull(LocaleContextHolder.getLocaleContext()); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); LocaleContextHolder.setDefaultLocale(Locale.GERMAN); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocale()); LocaleContextHolder.setDefaultLocale(null); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); }
LocaleContextHolder.setLocale(Locale.GERMANY); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); assertFalse(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); assertFalse(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getTimeZone("GMT+2"), LocaleContextHolder.getTimeZone()); assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getTimeZone("GMT+2"), LocaleContextHolder.getTimeZone()); assertTrue(LocaleContextHolder.getLocaleContext() instanceof TimeZoneAwareLocaleContext); assertNull(LocaleContextHolder.getLocaleContext().getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); assertNull(LocaleContextHolder.getLocaleContext());
/** * Return the TimeZone associated with the current thread, if any, * or the system default TimeZone otherwise. This is effectively a * replacement for {@link java.util.TimeZone#getDefault()}, * able to optionally respect a user-level TimeZone setting. * <p>Note: This method has a fallback to the shared default TimeZone, * either at the framework level or at the JVM-wide system level. * If you'd like to check for the raw LocaleContext content * (which may indicate no specific time zone through {@code null}, use * {@link #getLocaleContext()} and call {@link TimeZoneAwareLocaleContext#getTimeZone()} * after downcasting to {@link TimeZoneAwareLocaleContext}. * @return the current TimeZone, or the system default TimeZone if no * specific TimeZone has been associated with the current thread * @see #getLocaleContext() * @see TimeZoneAwareLocaleContext#getTimeZone() * @see #setDefaultTimeZone(TimeZone) * @see java.util.TimeZone#getDefault() */ public static TimeZone getTimeZone() { return getTimeZone(getLocaleContext()); }