diff --git a/spring-context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatterFactory.java b/spring-context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatterFactory.java index 7408e3c506a..0dc5fe6f0c0 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatterFactory.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatterFactory.java @@ -33,6 +33,7 @@ import org.springframework.util.StringUtils; * or {@link #setStyle(String) style} (considered in that order). * * @author Phillip Webb + * @author Sam Brannen * @see #getDateTimeFormatter() * @see #getDateTimeFormatter(DateTimeFormatter) * @since 3.2 @@ -100,7 +101,7 @@ public class DateTimeFormatterFactory implements FactoryBean public DateTimeFormatter getDateTimeFormatter(DateTimeFormatter fallbackFormatter) { DateTimeFormatter dateTimeFormatter = createDateTimeFormatter(); if(dateTimeFormatter != null && this.timeZone != null) { - dateTimeFormatter.withZone(DateTimeZone.forTimeZone(this.timeZone)); + dateTimeFormatter = dateTimeFormatter.withZone(DateTimeZone.forTimeZone(this.timeZone)); } return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter); } diff --git a/spring-context/src/test/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryTests.java b/spring-context/src/test/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryTests.java index 1a27b8d33bc..8457eb54fe9 100644 --- a/spring-context/src/test/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryTests.java +++ b/spring-context/src/test/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryTests.java @@ -16,16 +16,14 @@ package org.springframework.format.datetime.joda; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; -import static org.hamcrest.Matchers.sameInstance; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import java.util.Locale; import java.util.TimeZone; import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import org.junit.Test; @@ -35,6 +33,7 @@ import org.springframework.format.annotation.DateTimeFormat.ISO; * Tests for {@link DateTimeFormatterFactory}. * * @author Phillip Webb + * @author Sam Brannen */ public class DateTimeFormatterFactoryTests { @@ -42,6 +41,7 @@ public class DateTimeFormatterFactoryTests { private DateTime dateTime = new DateTime(2009, 10, 21, 12, 10, 00, 00); + @Test public void shouldDefaultToMediumFormat() throws Exception { assertThat(factory.getObject(), is(equalTo(DateTimeFormat.mediumDateTime()))); @@ -63,7 +63,7 @@ public class DateTimeFormatterFactoryTests { @Test @SuppressWarnings("rawtypes") public void shouldCreateDateTimeFormatter() throws Exception { - assertThat(factory.getObjectType(), is(equalTo((Class)DateTimeFormatter.class))); + assertThat(factory.getObjectType(), is(equalTo((Class) DateTimeFormatter.class))); } @Test @@ -93,12 +93,34 @@ public class DateTimeFormatterFactoryTests { @Test public void shouldGetWithTimeZone() throws Exception { + + TimeZone zurich = TimeZone.getTimeZone("Europe/Zurich"); + TimeZone newYork = TimeZone.getTimeZone("America/New_York"); + + // Ensure that we are testing against a timezone other than the default. + TimeZone testTimeZone; + String offset; + + if (zurich.equals(TimeZone.getDefault())) { + testTimeZone = newYork; + offset = "-0400"; // Daylight savings on October 21st + } + else { + testTimeZone = zurich; + offset = "+0200"; // Daylight savings on October 21st + } + factory.setPattern("yyyyMMddHHmmss Z"); - factory.setTimeZone(TimeZone.getTimeZone("-0700")); - assertThat(factory.getDateTimeFormatter().print(dateTime), is("20091021121000 -0700")); + factory.setTimeZone(testTimeZone); + + DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(testTimeZone); + DateTime dateTime = new DateTime(2009, 10, 21, 12, 10, 00, 00, dateTimeZone); + + assertThat(factory.getDateTimeFormatter().print(dateTime), is("20091021121000 " + offset)); } private DateTimeFormatter applyLocale(DateTimeFormatter dateTimeFormatter) { return dateTimeFormatter.withLocale(Locale.US); } + }