Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed May 21, 2024
1 parent fee17e1 commit 20dea0d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,12 +45,12 @@ public Instant parse(String text, Locale locale) throws ParseException {
return Instant.ofEpochMilli(Long.parseLong(text));
}
catch (NumberFormatException ex) {
if (text.length() > 0 && Character.isAlphabetic(text.charAt(0))) {
if (!text.isEmpty() && Character.isAlphabetic(text.charAt(0))) {
// assuming RFC-1123 value a la "Tue, 3 Jun 2008 11:05:30 GMT"
return Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(text));
}
else {
// assuming UTC instant a la "2007-12-03T10:15:30.00Z"
// assuming UTC instant a la "2007-12-03T10:15:30.000Z"
return Instant.parse(text);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,18 +644,18 @@ public static class DateTimeBean {
@DateTimeFormat(style = "M-")
private LocalDate styleLocalDate;

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

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

private LocalTime localTime;

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

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

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

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

private Instant instant;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Locale;
import java.util.Random;
import java.util.stream.Stream;

Expand Down Expand Up @@ -50,44 +51,39 @@ class InstantFormatterTests {

private final InstantFormatter instantFormatter = new InstantFormatter();


@ParameterizedTest
@ArgumentsSource(ISOSerializedInstantProvider.class)
void should_parse_an_ISO_formatted_string_representation_of_an_Instant(String input) throws ParseException {
Instant expected = DateTimeFormatter.ISO_INSTANT.parse(input, Instant::from);

Instant actual = instantFormatter.parse(input, null);

Instant actual = instantFormatter.parse(input, Locale.US);
assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@ArgumentsSource(RFC1123SerializedInstantProvider.class)
void should_parse_an_RFC1123_formatted_string_representation_of_an_Instant(String input) throws ParseException {
Instant expected = DateTimeFormatter.RFC_1123_DATE_TIME.parse(input, Instant::from);

Instant actual = instantFormatter.parse(input, null);

Instant actual = instantFormatter.parse(input, Locale.US);
assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@ArgumentsSource(RandomInstantProvider.class)
void should_serialize_an_Instant_using_ISO_format_and_ignoring_Locale(Instant input) {
String expected = DateTimeFormatter.ISO_INSTANT.format(input);

String actual = instantFormatter.print(input, null);

String actual = instantFormatter.print(input, Locale.US);
assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@ArgumentsSource(RandomEpochMillisProvider.class)
void should_parse_into_an_Instant_from_epoch_milli(Instant input) throws ParseException {
Instant actual = instantFormatter.parse(Long.toString(input.toEpochMilli()), null);

Instant actual = instantFormatter.parse(Long.toString(input.toEpochMilli()), Locale.US);
assertThat(actual).isEqualTo(input);
}


private static class RandomInstantProvider implements ArgumentsProvider {

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


private static class ISOSerializedInstantProvider extends RandomInstantProvider {

@Override
Expand All @@ -117,6 +114,7 @@ Stream<?> provideArguments() {
}
}


private static class RFC1123SerializedInstantProvider extends RandomInstantProvider {

// RFC-1123 supports only 4-digit years
Expand All @@ -130,6 +128,8 @@ Stream<?> provideArguments() {
.map(DateTimeFormatter.RFC_1123_DATE_TIME.withZone(systemDefault())::format);
}
}


private static final class RandomEpochMillisProvider implements ArgumentsProvider {

private static final long DATA_SET_SIZE = 10;
Expand Down

0 comments on commit 20dea0d

Please sign in to comment.