|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2021 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.
|
|
35 | 35 | *
|
36 | 36 | * @author Keith Donald
|
37 | 37 | * @author Phillip Webb
|
| 38 | + * @author Juergen Hoeller |
38 | 39 | */
|
39 |
| -public class DateFormatterTests { |
| 40 | +class DateFormatterTests { |
40 | 41 |
|
41 | 42 | private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
|
42 | 43 |
|
43 | 44 |
|
44 | 45 | @Test
|
45 |
| - public void shouldPrintAndParseDefault() throws Exception { |
| 46 | + void shouldPrintAndParseDefault() throws Exception { |
46 | 47 | DateFormatter formatter = new DateFormatter();
|
47 | 48 | formatter.setTimeZone(UTC);
|
| 49 | + |
48 | 50 | Date date = getDate(2009, Calendar.JUNE, 1);
|
49 | 51 | assertThat(formatter.print(date, Locale.US)).isEqualTo("Jun 1, 2009");
|
50 | 52 | assertThat(formatter.parse("Jun 1, 2009", Locale.US)).isEqualTo(date);
|
51 | 53 | }
|
52 | 54 |
|
53 | 55 | @Test
|
54 |
| - public void shouldPrintAndParseFromPattern() throws ParseException { |
| 56 | + void shouldPrintAndParseFromPattern() throws ParseException { |
55 | 57 | DateFormatter formatter = new DateFormatter("yyyy-MM-dd");
|
56 | 58 | formatter.setTimeZone(UTC);
|
| 59 | + |
57 | 60 | Date date = getDate(2009, Calendar.JUNE, 1);
|
58 | 61 | assertThat(formatter.print(date, Locale.US)).isEqualTo("2009-06-01");
|
59 | 62 | assertThat(formatter.parse("2009-06-01", Locale.US)).isEqualTo(date);
|
60 | 63 | }
|
61 | 64 |
|
62 | 65 | @Test
|
63 |
| - public void shouldPrintAndParseShort() throws Exception { |
| 66 | + void shouldPrintAndParseShort() throws Exception { |
64 | 67 | DateFormatter formatter = new DateFormatter();
|
65 | 68 | formatter.setTimeZone(UTC);
|
66 | 69 | formatter.setStyle(DateFormat.SHORT);
|
| 70 | + |
67 | 71 | Date date = getDate(2009, Calendar.JUNE, 1);
|
68 | 72 | assertThat(formatter.print(date, Locale.US)).isEqualTo("6/1/09");
|
69 | 73 | assertThat(formatter.parse("6/1/09", Locale.US)).isEqualTo(date);
|
70 | 74 | }
|
71 | 75 |
|
72 | 76 | @Test
|
73 |
| - public void shouldPrintAndParseMedium() throws Exception { |
| 77 | + void shouldPrintAndParseMedium() throws Exception { |
74 | 78 | DateFormatter formatter = new DateFormatter();
|
75 | 79 | formatter.setTimeZone(UTC);
|
76 | 80 | formatter.setStyle(DateFormat.MEDIUM);
|
| 81 | + |
77 | 82 | Date date = getDate(2009, Calendar.JUNE, 1);
|
78 | 83 | assertThat(formatter.print(date, Locale.US)).isEqualTo("Jun 1, 2009");
|
79 | 84 | assertThat(formatter.parse("Jun 1, 2009", Locale.US)).isEqualTo(date);
|
80 | 85 | }
|
81 | 86 |
|
82 | 87 | @Test
|
83 |
| - public void shouldPrintAndParseLong() throws Exception { |
| 88 | + void shouldPrintAndParseLong() throws Exception { |
84 | 89 | DateFormatter formatter = new DateFormatter();
|
85 | 90 | formatter.setTimeZone(UTC);
|
86 | 91 | formatter.setStyle(DateFormat.LONG);
|
| 92 | + |
87 | 93 | Date date = getDate(2009, Calendar.JUNE, 1);
|
88 | 94 | assertThat(formatter.print(date, Locale.US)).isEqualTo("June 1, 2009");
|
89 | 95 | assertThat(formatter.parse("June 1, 2009", Locale.US)).isEqualTo(date);
|
90 | 96 | }
|
91 | 97 |
|
92 | 98 | @Test
|
93 |
| - public void shouldPrintAndParseFull() throws Exception { |
| 99 | + void shouldPrintAndParseFull() throws Exception { |
94 | 100 | DateFormatter formatter = new DateFormatter();
|
95 | 101 | formatter.setTimeZone(UTC);
|
96 | 102 | formatter.setStyle(DateFormat.FULL);
|
| 103 | + |
97 | 104 | Date date = getDate(2009, Calendar.JUNE, 1);
|
98 | 105 | assertThat(formatter.print(date, Locale.US)).isEqualTo("Monday, June 1, 2009");
|
99 | 106 | assertThat(formatter.parse("Monday, June 1, 2009", Locale.US)).isEqualTo(date);
|
100 | 107 | }
|
101 | 108 |
|
102 | 109 | @Test
|
103 |
| - public void shouldPrintAndParseISODate() throws Exception { |
| 110 | + void shouldPrintAndParseIsoDate() throws Exception { |
104 | 111 | DateFormatter formatter = new DateFormatter();
|
105 | 112 | formatter.setTimeZone(UTC);
|
106 | 113 | formatter.setIso(ISO.DATE);
|
| 114 | + |
107 | 115 | Date date = getDate(2009, Calendar.JUNE, 1, 14, 23, 5, 3);
|
108 | 116 | assertThat(formatter.print(date, Locale.US)).isEqualTo("2009-06-01");
|
109 | 117 | assertThat(formatter.parse("2009-6-01", Locale.US))
|
110 | 118 | .isEqualTo(getDate(2009, Calendar.JUNE, 1));
|
111 | 119 | }
|
112 | 120 |
|
113 | 121 | @Test
|
114 |
| - public void shouldPrintAndParseISOTime() throws Exception { |
| 122 | + void shouldPrintAndParseIsoTime() throws Exception { |
115 | 123 | DateFormatter formatter = new DateFormatter();
|
116 | 124 | formatter.setTimeZone(UTC);
|
117 | 125 | formatter.setIso(ISO.TIME);
|
| 126 | + |
118 | 127 | Date date = getDate(2009, Calendar.JANUARY, 1, 14, 23, 5, 3);
|
119 | 128 | assertThat(formatter.print(date, Locale.US)).isEqualTo("14:23:05.003Z");
|
120 | 129 | assertThat(formatter.parse("14:23:05.003Z", Locale.US))
|
121 | 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()); |
122 | 136 | }
|
123 | 137 |
|
124 | 138 | @Test
|
125 |
| - public void shouldPrintAndParseISODateTime() throws Exception { |
| 139 | + void shouldPrintAndParseIsoDateTime() throws Exception { |
126 | 140 | DateFormatter formatter = new DateFormatter();
|
127 | 141 | formatter.setTimeZone(UTC);
|
128 | 142 | formatter.setIso(ISO.DATE_TIME);
|
| 143 | + |
129 | 144 | Date date = getDate(2009, Calendar.JUNE, 1, 14, 23, 5, 3);
|
130 | 145 | assertThat(formatter.print(date, Locale.US)).isEqualTo("2009-06-01T14:23:05.003Z");
|
131 | 146 | assertThat(formatter.parse("2009-06-01T14:23:05.003Z", Locale.US)).isEqualTo(date);
|
| 147 | + |
| 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()); |
132 | 151 | }
|
133 | 152 |
|
134 | 153 | @Test
|
135 |
| - public void shouldThrowOnUnsupportedStylePattern() throws Exception { |
| 154 | + void shouldThrowOnUnsupportedStylePattern() { |
136 | 155 | DateFormatter formatter = new DateFormatter();
|
137 | 156 | formatter.setStylePattern("OO");
|
138 |
| - assertThatIllegalStateException().isThrownBy(() -> |
139 |
| - formatter.parse("2009", Locale.US)) |
140 |
| - .withMessageContaining("Unsupported style pattern 'OO'"); |
| 157 | + |
| 158 | + assertThatIllegalStateException().isThrownBy(() -> formatter.parse("2009", Locale.US)) |
| 159 | + .withMessageContaining("Unsupported style pattern 'OO'"); |
141 | 160 | }
|
142 | 161 |
|
143 | 162 | @Test
|
144 |
| - public void shouldUseCorrectOrder() throws Exception { |
| 163 | + void shouldUseCorrectOrder() { |
145 | 164 | DateFormatter formatter = new DateFormatter();
|
146 | 165 | formatter.setTimeZone(UTC);
|
147 | 166 | formatter.setStyle(DateFormat.SHORT);
|
148 | 167 | formatter.setStylePattern("L-");
|
149 | 168 | formatter.setIso(ISO.DATE_TIME);
|
150 | 169 | formatter.setPattern("yyyy");
|
151 |
| - Date date = getDate(2009, Calendar.JUNE, 1, 14, 23, 5, 3); |
152 | 170 |
|
| 171 | + Date date = getDate(2009, Calendar.JUNE, 1, 14, 23, 5, 3); |
153 | 172 | assertThat(formatter.print(date, Locale.US)).as("uses pattern").isEqualTo("2009");
|
154 | 173 |
|
155 | 174 | formatter.setPattern("");
|
|
0 commit comments