Skip to content

Commit 20dea0d

Browse files
committed
Polishing
1 parent fee17e1 commit 20dea0d

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

spring-context/src/main/java/org/springframework/format/datetime/standard/InstantFormatter.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,12 +45,12 @@ public Instant parse(String text, Locale locale) throws ParseException {
4545
return Instant.ofEpochMilli(Long.parseLong(text));
4646
}
4747
catch (NumberFormatException ex) {
48-
if (text.length() > 0 && Character.isAlphabetic(text.charAt(0))) {
48+
if (!text.isEmpty() && Character.isAlphabetic(text.charAt(0))) {
4949
// assuming RFC-1123 value a la "Tue, 3 Jun 2008 11:05:30 GMT"
5050
return Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(text));
5151
}
5252
else {
53-
// assuming UTC instant a la "2007-12-03T10:15:30.00Z"
53+
// assuming UTC instant a la "2007-12-03T10:15:30.000Z"
5454
return Instant.parse(text);
5555
}
5656
}

spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -644,18 +644,18 @@ public static class DateTimeBean {
644644
@DateTimeFormat(style = "M-")
645645
private LocalDate styleLocalDate;
646646

647-
@DateTimeFormat(style = "S-", fallbackPatterns = { "yyyy-MM-dd", "yyyyMMdd", "yyyy.MM.dd" })
647+
@DateTimeFormat(style = "S-", fallbackPatterns = {"yyyy-MM-dd", "yyyyMMdd", "yyyy.MM.dd"})
648648
private LocalDate styleLocalDateWithFallbackPatterns;
649649

650-
@DateTimeFormat(pattern = "yyyy-MM-dd", fallbackPatterns = { "M/d/yy", "yyyyMMdd", "yyyy.MM.dd" })
650+
@DateTimeFormat(pattern = "yyyy-MM-dd", fallbackPatterns = {"M/d/yy", "yyyyMMdd", "yyyy.MM.dd"})
651651
private LocalDate patternLocalDateWithFallbackPatterns;
652652

653653
private LocalTime localTime;
654654

655655
@DateTimeFormat(style = "-M")
656656
private LocalTime styleLocalTime;
657657

658-
@DateTimeFormat(style = "-M", fallbackPatterns = { "HH:mm:ss", "HH:mm"})
658+
@DateTimeFormat(style = "-M", fallbackPatterns = {"HH:mm:ss", "HH:mm"})
659659
private LocalTime styleLocalTimeWithFallbackPatterns;
660660

661661
private LocalDateTime localDateTime;
@@ -675,7 +675,7 @@ public static class DateTimeBean {
675675
@DateTimeFormat(iso = ISO.DATE_TIME)
676676
private LocalDateTime isoLocalDateTime;
677677

678-
@DateTimeFormat(iso = ISO.DATE_TIME, fallbackPatterns = { "yyyy-MM-dd HH:mm:ss", "M/d/yy HH:mm"})
678+
@DateTimeFormat(iso = ISO.DATE_TIME, fallbackPatterns = {"yyyy-MM-dd HH:mm:ss", "M/d/yy HH:mm"})
679679
private LocalDateTime isoLocalDateTimeWithFallbackPatterns;
680680

681681
private Instant instant;

spring-context/src/test/java/org/springframework/format/datetime/standard/InstantFormatterTests.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.time.Instant;
2121
import java.time.format.DateTimeFormatter;
2222
import java.time.temporal.ChronoUnit;
23+
import java.util.Locale;
2324
import java.util.Random;
2425
import java.util.stream.Stream;
2526

@@ -50,44 +51,39 @@ class InstantFormatterTests {
5051

5152
private final InstantFormatter instantFormatter = new InstantFormatter();
5253

54+
5355
@ParameterizedTest
5456
@ArgumentsSource(ISOSerializedInstantProvider.class)
5557
void should_parse_an_ISO_formatted_string_representation_of_an_Instant(String input) throws ParseException {
5658
Instant expected = DateTimeFormatter.ISO_INSTANT.parse(input, Instant::from);
57-
58-
Instant actual = instantFormatter.parse(input, null);
59-
59+
Instant actual = instantFormatter.parse(input, Locale.US);
6060
assertThat(actual).isEqualTo(expected);
6161
}
6262

6363
@ParameterizedTest
6464
@ArgumentsSource(RFC1123SerializedInstantProvider.class)
6565
void should_parse_an_RFC1123_formatted_string_representation_of_an_Instant(String input) throws ParseException {
6666
Instant expected = DateTimeFormatter.RFC_1123_DATE_TIME.parse(input, Instant::from);
67-
68-
Instant actual = instantFormatter.parse(input, null);
69-
67+
Instant actual = instantFormatter.parse(input, Locale.US);
7068
assertThat(actual).isEqualTo(expected);
7169
}
7270

7371
@ParameterizedTest
7472
@ArgumentsSource(RandomInstantProvider.class)
7573
void should_serialize_an_Instant_using_ISO_format_and_ignoring_Locale(Instant input) {
7674
String expected = DateTimeFormatter.ISO_INSTANT.format(input);
77-
78-
String actual = instantFormatter.print(input, null);
79-
75+
String actual = instantFormatter.print(input, Locale.US);
8076
assertThat(actual).isEqualTo(expected);
8177
}
8278

8379
@ParameterizedTest
8480
@ArgumentsSource(RandomEpochMillisProvider.class)
8581
void should_parse_into_an_Instant_from_epoch_milli(Instant input) throws ParseException {
86-
Instant actual = instantFormatter.parse(Long.toString(input.toEpochMilli()), null);
87-
82+
Instant actual = instantFormatter.parse(Long.toString(input.toEpochMilli()), Locale.US);
8883
assertThat(actual).isEqualTo(input);
8984
}
9085

86+
9187
private static class RandomInstantProvider implements ArgumentsProvider {
9288

9389
private static final long DATA_SET_SIZE = 10;
@@ -109,6 +105,7 @@ Stream<Instant> randomInstantStream(Instant min, Instant max) {
109105
}
110106
}
111107

108+
112109
private static class ISOSerializedInstantProvider extends RandomInstantProvider {
113110

114111
@Override
@@ -117,6 +114,7 @@ Stream<?> provideArguments() {
117114
}
118115
}
119116

117+
120118
private static class RFC1123SerializedInstantProvider extends RandomInstantProvider {
121119

122120
// RFC-1123 supports only 4-digit years
@@ -130,6 +128,8 @@ Stream<?> provideArguments() {
130128
.map(DateTimeFormatter.RFC_1123_DATE_TIME.withZone(systemDefault())::format);
131129
}
132130
}
131+
132+
133133
private static final class RandomEpochMillisProvider implements ArgumentsProvider {
134134

135135
private static final long DATA_SET_SIZE = 10;

0 commit comments

Comments
 (0)