Last active
July 1, 2024 15:41
-
-
Save Megaprog/0cea7e5ba15b475e840b0de519b90fbe to your computer and use it in GitHub Desktop.
RFC3339 parsing by java.time (only parsing is possible)
This file contains 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
package rfc3339; | |
import org.junit.jupiter.api.Assertions; | |
import org.junit.jupiter.api.Test; | |
import org.junit.jupiter.api.function.Executable; | |
import java.time.ZoneOffset; | |
import java.time.ZonedDateTime; | |
import java.time.format.DateTimeFormatter; | |
import java.time.format.ResolverStyle; | |
public class Rfc3339Test { | |
public static final DateTimeFormatter rfc3339Formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[.SSS]XXX") | |
.withResolverStyle(ResolverStyle.LENIENT); | |
public static ZonedDateTime parseRfc3339(String rfcDateTime) { | |
return ZonedDateTime.parse(rfcDateTime, rfc3339Formatter); | |
} | |
@Test void testHourWithTimezone() { | |
int hour = parseRfc3339("2019-07-19T10:14:39.812+01:00").withZoneSameInstant(ZoneOffset.UTC).getHour(); | |
Assertions.assertEquals(9, hour); | |
} | |
@Test void testUnknownLocalOffset() { | |
int hour = parseRfc3339("2019-07-19T10:14:39.812-00:00").withZoneSameInstant(ZoneOffset.UTC).getHour(); | |
Assertions.assertEquals(10, hour); | |
System.out.println(parseRfc3339("1990-12-31T23:59:60Z")); | |
System.out.println(parseRfc3339("1990-12-31T15:59:60-08:00")); | |
} | |
/** | |
* Examples are taken from <a href="https://www.ietf.org/rfc/rfc3339.txt"> RFC 3339 standard </a> | |
*/ | |
@Test void testParsingDifferentDateTimes() { | |
Assertions.assertDoesNotThrow(exParseRfc3339("1985-04-12T23:20:50.52Z")); | |
Assertions.assertDoesNotThrow(exParseRfc3339("1996-12-19T16:39:57-08:00")); | |
Assertions.assertDoesNotThrow(exParseRfc3339("1990-12-31T23:59:60Z")); | |
Assertions.assertDoesNotThrow(exParseRfc3339("1990-12-31T15:59:60-08:00")); | |
Assertions.assertDoesNotThrow(exParseRfc3339("1937-01-01T12:00:27.87+00:20")); | |
} | |
private static Executable exParseRfc3339(String toParse) { | |
return () -> parseRfc3339(toParse); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
According https://tools.ietf.org/html/rfc3339
https://stackoverflow.com/a/55310635/1306553
https://stackoverflow.com/a/49634501/1306553