Fix timezone issue in DateTimeFormatterFactory
The DateTimeFormatterFactory introduced in SPR-7121 supports a timeZone property; however, this property is currently not properly supported. This commit addresses this issue by ensuring that the timeZone properly is honored. Issue: SPR-9953
This commit is contained in:
parent
cef5f0222e
commit
93c01e0710
|
|
@ -33,6 +33,7 @@ import org.springframework.util.StringUtils;
|
||||||
* or {@link #setStyle(String) style} (considered in that order).
|
* or {@link #setStyle(String) style} (considered in that order).
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Sam Brannen
|
||||||
* @see #getDateTimeFormatter()
|
* @see #getDateTimeFormatter()
|
||||||
* @see #getDateTimeFormatter(DateTimeFormatter)
|
* @see #getDateTimeFormatter(DateTimeFormatter)
|
||||||
* @since 3.2
|
* @since 3.2
|
||||||
|
|
@ -100,7 +101,7 @@ public class DateTimeFormatterFactory implements FactoryBean<DateTimeFormatter>
|
||||||
public DateTimeFormatter getDateTimeFormatter(DateTimeFormatter fallbackFormatter) {
|
public DateTimeFormatter getDateTimeFormatter(DateTimeFormatter fallbackFormatter) {
|
||||||
DateTimeFormatter dateTimeFormatter = createDateTimeFormatter();
|
DateTimeFormatter dateTimeFormatter = createDateTimeFormatter();
|
||||||
if(dateTimeFormatter != null && this.timeZone != null) {
|
if(dateTimeFormatter != null && this.timeZone != null) {
|
||||||
dateTimeFormatter.withZone(DateTimeZone.forTimeZone(this.timeZone));
|
dateTimeFormatter = dateTimeFormatter.withZone(DateTimeZone.forTimeZone(this.timeZone));
|
||||||
}
|
}
|
||||||
return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter);
|
return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,16 +16,14 @@
|
||||||
|
|
||||||
package org.springframework.format.datetime.joda;
|
package org.springframework.format.datetime.joda;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.hamcrest.Matchers.is;
|
|
||||||
import static org.hamcrest.Matchers.nullValue;
|
|
||||||
import static org.hamcrest.Matchers.sameInstance;
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
|
import org.joda.time.DateTimeZone;
|
||||||
import org.joda.time.format.DateTimeFormat;
|
import org.joda.time.format.DateTimeFormat;
|
||||||
import org.joda.time.format.DateTimeFormatter;
|
import org.joda.time.format.DateTimeFormatter;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
@ -35,6 +33,7 @@ import org.springframework.format.annotation.DateTimeFormat.ISO;
|
||||||
* Tests for {@link DateTimeFormatterFactory}.
|
* Tests for {@link DateTimeFormatterFactory}.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Sam Brannen
|
||||||
*/
|
*/
|
||||||
public class DateTimeFormatterFactoryTests {
|
public class DateTimeFormatterFactoryTests {
|
||||||
|
|
||||||
|
|
@ -42,6 +41,7 @@ public class DateTimeFormatterFactoryTests {
|
||||||
|
|
||||||
private DateTime dateTime = new DateTime(2009, 10, 21, 12, 10, 00, 00);
|
private DateTime dateTime = new DateTime(2009, 10, 21, 12, 10, 00, 00);
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldDefaultToMediumFormat() throws Exception {
|
public void shouldDefaultToMediumFormat() throws Exception {
|
||||||
assertThat(factory.getObject(), is(equalTo(DateTimeFormat.mediumDateTime())));
|
assertThat(factory.getObject(), is(equalTo(DateTimeFormat.mediumDateTime())));
|
||||||
|
|
@ -63,7 +63,7 @@ public class DateTimeFormatterFactoryTests {
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
public void shouldCreateDateTimeFormatter() throws Exception {
|
public void shouldCreateDateTimeFormatter() throws Exception {
|
||||||
assertThat(factory.getObjectType(), is(equalTo((Class)DateTimeFormatter.class)));
|
assertThat(factory.getObjectType(), is(equalTo((Class) DateTimeFormatter.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -93,12 +93,34 @@ public class DateTimeFormatterFactoryTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldGetWithTimeZone() throws Exception {
|
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.setPattern("yyyyMMddHHmmss Z");
|
||||||
factory.setTimeZone(TimeZone.getTimeZone("-0700"));
|
factory.setTimeZone(testTimeZone);
|
||||||
assertThat(factory.getDateTimeFormatter().print(dateTime), is("20091021121000 -0700"));
|
|
||||||
|
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) {
|
private DateTimeFormatter applyLocale(DateTimeFormatter dateTimeFormatter) {
|
||||||
return dateTimeFormatter.withLocale(Locale.US);
|
return dateTimeFormatter.withLocale(Locale.US);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue