|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
23 | 23 | import java.util.Locale;
|
24 | 24 | import java.util.TimeZone;
|
25 | 25 |
|
26 |
| -import org.joda.time.DateTimeZone; |
27 |
| -import org.joda.time.format.DateTimeFormat; |
28 |
| -import org.joda.time.format.DateTimeFormatter; |
29 | 26 | import org.junit.jupiter.api.Test;
|
30 | 27 |
|
31 | 28 | import org.springframework.format.annotation.DateTimeFormat.ISO;
|
32 | 29 |
|
33 | 30 | import static org.assertj.core.api.Assertions.assertThat;
|
34 | 31 | import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
35 | 32 |
|
36 |
| - |
37 |
| - |
38 |
| - |
39 | 33 | /**
|
40 | 34 | * Tests for {@link DateFormatter}.
|
41 | 35 | *
|
42 | 36 | * @author Keith Donald
|
43 | 37 | * @author Phillip Webb
|
| 38 | + * @author Juergen Hoeller |
44 | 39 | */
|
45 |
| -public class DateFormatterTests { |
| 40 | +class DateFormatterTests { |
46 | 41 |
|
47 | 42 | private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
|
48 | 43 |
|
49 | 44 |
|
50 | 45 | @Test
|
51 |
| - public void shouldPrintAndParseDefault() throws Exception { |
| 46 | + void shouldPrintAndParseDefault() throws Exception { |
52 | 47 | DateFormatter formatter = new DateFormatter();
|
53 | 48 | formatter.setTimeZone(UTC);
|
| 49 | + |
54 | 50 | Date date = getDate(2009, Calendar.JUNE, 1);
|
55 | 51 | assertThat(formatter.print(date, Locale.US)).isEqualTo("Jun 1, 2009");
|
56 | 52 | assertThat(formatter.parse("Jun 1, 2009", Locale.US)).isEqualTo(date);
|
57 | 53 | }
|
58 | 54 |
|
59 | 55 | @Test
|
60 |
| - public void shouldPrintAndParseFromPattern() throws ParseException { |
| 56 | + void shouldPrintAndParseFromPattern() throws ParseException { |
61 | 57 | DateFormatter formatter = new DateFormatter("yyyy-MM-dd");
|
62 | 58 | formatter.setTimeZone(UTC);
|
| 59 | + |
63 | 60 | Date date = getDate(2009, Calendar.JUNE, 1);
|
64 | 61 | assertThat(formatter.print(date, Locale.US)).isEqualTo("2009-06-01");
|
65 | 62 | assertThat(formatter.parse("2009-06-01", Locale.US)).isEqualTo(date);
|
66 | 63 | }
|
67 | 64 |
|
68 | 65 | @Test
|
69 |
| - public void shouldPrintAndParseShort() throws Exception { |
| 66 | + void shouldPrintAndParseShort() throws Exception { |
70 | 67 | DateFormatter formatter = new DateFormatter();
|
71 | 68 | formatter.setTimeZone(UTC);
|
72 | 69 | formatter.setStyle(DateFormat.SHORT);
|
| 70 | + |
73 | 71 | Date date = getDate(2009, Calendar.JUNE, 1);
|
74 | 72 | assertThat(formatter.print(date, Locale.US)).isEqualTo("6/1/09");
|
75 | 73 | assertThat(formatter.parse("6/1/09", Locale.US)).isEqualTo(date);
|
76 | 74 | }
|
77 | 75 |
|
78 | 76 | @Test
|
79 |
| - public void shouldPrintAndParseMedium() throws Exception { |
| 77 | + void shouldPrintAndParseMedium() throws Exception { |
80 | 78 | DateFormatter formatter = new DateFormatter();
|
81 | 79 | formatter.setTimeZone(UTC);
|
82 | 80 | formatter.setStyle(DateFormat.MEDIUM);
|
| 81 | + |
83 | 82 | Date date = getDate(2009, Calendar.JUNE, 1);
|
84 | 83 | assertThat(formatter.print(date, Locale.US)).isEqualTo("Jun 1, 2009");
|
85 | 84 | assertThat(formatter.parse("Jun 1, 2009", Locale.US)).isEqualTo(date);
|
86 | 85 | }
|
87 | 86 |
|
88 | 87 | @Test
|
89 |
| - public void shouldPrintAndParseLong() throws Exception { |
| 88 | + void shouldPrintAndParseLong() throws Exception { |
90 | 89 | DateFormatter formatter = new DateFormatter();
|
91 | 90 | formatter.setTimeZone(UTC);
|
92 | 91 | formatter.setStyle(DateFormat.LONG);
|
| 92 | + |
93 | 93 | Date date = getDate(2009, Calendar.JUNE, 1);
|
94 | 94 | assertThat(formatter.print(date, Locale.US)).isEqualTo("June 1, 2009");
|
95 | 95 | assertThat(formatter.parse("June 1, 2009", Locale.US)).isEqualTo(date);
|
96 | 96 | }
|
97 | 97 |
|
98 | 98 | @Test
|
99 |
| - public void shouldPrintAndParseFull() throws Exception { |
| 99 | + void shouldPrintAndParseFull() throws Exception { |
100 | 100 | DateFormatter formatter = new DateFormatter();
|
101 | 101 | formatter.setTimeZone(UTC);
|
102 | 102 | formatter.setStyle(DateFormat.FULL);
|
| 103 | + |
103 | 104 | Date date = getDate(2009, Calendar.JUNE, 1);
|
104 | 105 | assertThat(formatter.print(date, Locale.US)).isEqualTo("Monday, June 1, 2009");
|
105 | 106 | assertThat(formatter.parse("Monday, June 1, 2009", Locale.US)).isEqualTo(date);
|
106 | 107 | }
|
107 | 108 |
|
108 | 109 | @Test
|
109 |
| - public void shouldPrintAndParseISODate() throws Exception { |
| 110 | + void shouldPrintAndParseIsoDate() throws Exception { |
110 | 111 | DateFormatter formatter = new DateFormatter();
|
111 | 112 | formatter.setTimeZone(UTC);
|
112 | 113 | formatter.setIso(ISO.DATE);
|
| 114 | + |
113 | 115 | Date date = getDate(2009, Calendar.JUNE, 1, 14, 23, 5, 3);
|
114 | 116 | assertThat(formatter.print(date, Locale.US)).isEqualTo("2009-06-01");
|
115 | 117 | assertThat(formatter.parse("2009-6-01", Locale.US))
|
116 | 118 | .isEqualTo(getDate(2009, Calendar.JUNE, 1));
|
117 | 119 | }
|
118 | 120 |
|
119 | 121 | @Test
|
120 |
| - public void shouldPrintAndParseISOTime() throws Exception { |
| 122 | + void shouldPrintAndParseIsoTime() throws Exception { |
121 | 123 | DateFormatter formatter = new DateFormatter();
|
122 | 124 | formatter.setTimeZone(UTC);
|
123 | 125 | formatter.setIso(ISO.TIME);
|
| 126 | + |
124 | 127 | Date date = getDate(2009, Calendar.JANUARY, 1, 14, 23, 5, 3);
|
125 | 128 | assertThat(formatter.print(date, Locale.US)).isEqualTo("14:23:05.003Z");
|
126 | 129 | assertThat(formatter.parse("14:23:05.003Z", Locale.US))
|
127 | 130 | .isEqualTo(getDate(1970, Calendar.JANUARY, 1, 14, 23, 5, 3));
|
| 131 | + |
| 132 | + date = getDate(2009, Calendar.JANUARY, 1, 14, 23, 5, 0); |
| 133 | + assertThat(formatter.print(date, Locale.US)).isEqualTo("14:23:05.000Z"); |
| 134 | + assertThat(formatter.parse("14:23:05Z", Locale.US)) |
| 135 | + .isEqualTo(getDate(1970, Calendar.JANUARY, 1, 14, 23, 5, 0).toInstant()); |
128 | 136 | }
|
129 | 137 |
|
130 | 138 | @Test
|
131 |
| - public void shouldPrintAndParseISODateTime() throws Exception { |
| 139 | + void shouldPrintAndParseIsoDateTime() throws Exception { |
132 | 140 | DateFormatter formatter = new DateFormatter();
|
133 | 141 | formatter.setTimeZone(UTC);
|
134 | 142 | formatter.setIso(ISO.DATE_TIME);
|
| 143 | + |
135 | 144 | Date date = getDate(2009, Calendar.JUNE, 1, 14, 23, 5, 3);
|
136 | 145 | assertThat(formatter.print(date, Locale.US)).isEqualTo("2009-06-01T14:23:05.003Z");
|
137 | 146 | assertThat(formatter.parse("2009-06-01T14:23:05.003Z", Locale.US)).isEqualTo(date);
|
138 |
| - } |
139 | 147 |
|
140 |
| - @Test |
141 |
| - public void shouldSupportJodaStylePatterns() throws Exception { |
142 |
| - String[] chars = { "S", "M", "-" }; |
143 |
| - for (String d : chars) { |
144 |
| - for (String t : chars) { |
145 |
| - String style = d + t; |
146 |
| - if (!style.equals("--")) { |
147 |
| - Date date = getDate(2009, Calendar.JUNE, 10, 14, 23, 0, 0); |
148 |
| - if (t.equals("-")) { |
149 |
| - date = getDate(2009, Calendar.JUNE, 10); |
150 |
| - } |
151 |
| - else if (d.equals("-")) { |
152 |
| - date = getDate(1970, Calendar.JANUARY, 1, 14, 23, 0, 0); |
153 |
| - } |
154 |
| - testJodaStylePatterns(style, Locale.US, date); |
155 |
| - } |
156 |
| - } |
157 |
| - } |
158 |
| - } |
159 |
| - |
160 |
| - private void testJodaStylePatterns(String style, Locale locale, Date date) throws Exception { |
161 |
| - DateFormatter formatter = new DateFormatter(); |
162 |
| - formatter.setTimeZone(UTC); |
163 |
| - formatter.setStylePattern(style); |
164 |
| - DateTimeFormatter jodaFormatter = DateTimeFormat.forStyle(style).withLocale(locale).withZone(DateTimeZone.UTC); |
165 |
| - String jodaPrinted = jodaFormatter.print(date.getTime()); |
166 |
| - assertThat(formatter.print(date, locale)) |
167 |
| - .as("Unable to print style pattern " + style) |
168 |
| - .isEqualTo(jodaPrinted); |
169 |
| - assertThat(formatter.parse(jodaPrinted, locale)) |
170 |
| - .as("Unable to parse style pattern " + style) |
171 |
| - .isEqualTo(date); |
| 148 | + date = getDate(2009, Calendar.JUNE, 1, 14, 23, 5, 0); |
| 149 | + assertThat(formatter.print(date, Locale.US)).isEqualTo("2009-06-01T14:23:05.000Z"); |
| 150 | + assertThat(formatter.parse("2009-06-01T14:23:05Z", Locale.US)).isEqualTo(date.toInstant()); |
172 | 151 | }
|
173 | 152 |
|
174 | 153 | @Test
|
175 |
| - public void shouldThrowOnUnsupportedStylePattern() throws Exception { |
| 154 | + void shouldThrowOnUnsupportedStylePattern() { |
176 | 155 | DateFormatter formatter = new DateFormatter();
|
177 | 156 | formatter.setStylePattern("OO");
|
178 |
| - assertThatIllegalStateException().isThrownBy(() -> |
179 |
| - formatter.parse("2009", Locale.US)) |
180 |
| - .withMessageContaining("Unsupported style pattern 'OO'"); |
| 157 | + |
| 158 | + assertThatIllegalStateException().isThrownBy(() -> formatter.parse("2009", Locale.US)) |
| 159 | + .withMessageContaining("Unsupported style pattern 'OO'"); |
181 | 160 | }
|
182 | 161 |
|
183 | 162 | @Test
|
184 |
| - public void shouldUseCorrectOrder() throws Exception { |
| 163 | + void shouldUseCorrectOrder() { |
185 | 164 | DateFormatter formatter = new DateFormatter();
|
186 | 165 | formatter.setTimeZone(UTC);
|
187 | 166 | formatter.setStyle(DateFormat.SHORT);
|
188 | 167 | formatter.setStylePattern("L-");
|
189 | 168 | formatter.setIso(ISO.DATE_TIME);
|
190 | 169 | formatter.setPattern("yyyy");
|
191 |
| - Date date = getDate(2009, Calendar.JUNE, 1, 14, 23, 5, 3); |
192 | 170 |
|
| 171 | + Date date = getDate(2009, Calendar.JUNE, 1, 14, 23, 5, 3); |
193 | 172 | assertThat(formatter.print(date, Locale.US)).as("uses pattern").isEqualTo("2009");
|
194 | 173 |
|
195 | 174 | formatter.setPattern("");
|
|
0 commit comments