Last active
September 13, 2023 12:59
-
-
Save tomwhoiscontrary/fd5c01d0a3f2f9e37be2843c9a6c89a9 to your computer and use it in GitHub Desktop.
trying out the nice ordinal formatting and parsing code in https://stackoverflow.com/a/72557618/116639
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
str = Sep 8th 2023 | |
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Sep 8th 2023' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2023-09-08 of type java.time.format.Parsed | |
at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2023) | |
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1958) | |
at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:494) | |
at ParseOrdinalDate.main(ParseOrdinalDate.java:41) | |
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2023-09-08 of type java.time.format.Parsed | |
at java.base/java.time.LocalDateTime.from(LocalDateTime.java:463) | |
at java.base/java.time.format.Parsed.query(Parsed.java:241) | |
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954) | |
... 2 more | |
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {},ISO resolved to 2023-09-08 of type java.time.format.Parsed | |
at java.base/java.time.LocalTime.from(LocalTime.java:433) | |
at java.base/java.time.LocalDateTime.from(LocalDateTime.java:459) | |
... 4 more |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalDate; | |
import java.time.LocalDateTime; | |
import java.time.format.DateTimeFormatter; | |
import java.time.format.DateTimeFormatterBuilder; | |
import java.time.format.TextStyle; | |
import java.time.temporal.ChronoField; | |
import java.util.HashMap; | |
import java.util.Locale; | |
import java.util.Map; | |
public class ParseOrdinalDate { | |
static final Map<Long, String> ORDINAL_SUFFIX_MAP; | |
static { | |
Map<Long, String> map = new HashMap<>(); | |
for (int i = 1; i <= 31; i++) { | |
String suffix = switch (i) { | |
case 1, 21, 31 -> "st"; | |
case 2, 22 -> "nd"; | |
case 3, 23 -> "rd"; | |
default -> "th"; | |
}; | |
map.put((long) i, i + suffix); | |
} | |
ORDINAL_SUFFIX_MAP = Map.copyOf(map); | |
} | |
public static void main(String[] args) { | |
DateTimeFormatter formatter = new DateTimeFormatterBuilder() | |
.appendText(ChronoField.MONTH_OF_YEAR, TextStyle.SHORT) | |
.appendLiteral(" ") | |
.appendText(ChronoField.DAY_OF_MONTH, ORDINAL_SUFFIX_MAP) | |
.appendLiteral(" ") | |
.appendText(ChronoField.YEAR, TextStyle.SHORT) | |
.toFormatter(Locale.ROOT); | |
String str = formatter.format(LocalDate.now()); | |
System.out.println("str = " + str); | |
LocalDateTime result = LocalDateTime.parse(str, formatter); | |
System.out.println("result = " + result); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalDate; | |
import java.time.format.DateTimeFormatter; | |
import java.time.format.DateTimeFormatterBuilder; | |
import java.time.format.TextStyle; | |
import java.time.temporal.ChronoField; | |
import java.util.Locale; | |
import java.util.Map; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
public class ParseOrdinalDateFixed { | |
static final Map<Long, String> ORDINAL_SUFFIX_MAP = IntStream.rangeClosed(1, 31) | |
.boxed() | |
.collect(Collectors.toMap(Integer::longValue, | |
n -> n + switch ((n % 100 / 10 != 1) ? n % 10 : 0) { | |
case 1 -> "st"; | |
case 2 -> "nd"; | |
case 3 -> "rd"; | |
default -> "th"; | |
})); | |
public static void main(String[] args) { | |
DateTimeFormatter formatter = new DateTimeFormatterBuilder() | |
.appendText(ChronoField.MONTH_OF_YEAR, TextStyle.SHORT) | |
.appendLiteral(" ") | |
.appendText(ChronoField.DAY_OF_MONTH, ORDINAL_SUFFIX_MAP) | |
.appendLiteral(" ") | |
.appendText(ChronoField.YEAR, TextStyle.SHORT) | |
.toFormatter(Locale.ROOT); | |
for (LocalDate date = LocalDate.of(2000, 1, 1); date.isBefore(LocalDate.of(2000, 2, 1)); date = date.plusDays(1)) { | |
String str = formatter.format(date); | |
LocalDate result = LocalDate.parse(str, formatter); | |
boolean same = result.equals(date); | |
System.out.println("date = " + date + ", str = " + str + ", result = " + result + ", same = " + same); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment