Add support for common RSS date format without timezone

This commit is contained in:
Shinokuni 2020-07-18 14:31:54 +02:00
parent 465838d4b6
commit b54c80437d
2 changed files with 10 additions and 2 deletions

View File

@ -15,7 +15,7 @@ public final class DateUtils {
* Fri, 04 Jan 2019 22:21:46 GMT
* Fri, 04 Jan 2019 22:21:46 +0000
*/
private static final String RSS_2_BASE_PATTERN = "EEE, dd MMM yyyy HH:mm:ss ";
private static final String RSS_2_BASE_PATTERN = "EEE, dd MMM yyyy HH:mm:ss";
private static final String GMT_PATTERN = "ZZZ";
@ -32,7 +32,8 @@ public final class DateUtils {
public static LocalDateTime stringToLocalDateTime(String value) {
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormat.forPattern(RSS_2_BASE_PATTERN).getParser())
.appendOptional(DateTimeFormat.forPattern(RSS_2_BASE_PATTERN + " ").getParser()) // with timezone
.appendOptional(DateTimeFormat.forPattern(RSS_2_BASE_PATTERN).getParser()) // no timezone, important order here
.appendOptional(DateTimeFormat.forPattern(ATOM_JSON_DATE_FORMAT).getParser())
.appendOptional(DateTimeFormat.forPattern(GMT_PATTERN).getParser())
.appendOptional(DateTimeFormat.forPattern(OFFSET_PATTERN).getParser())

View File

@ -23,6 +23,13 @@ public class DateUtilsTest {
assertEquals(0, dateTime.compareTo(DateUtils.stringToLocalDateTime("Fri, 04 Jan 2019 22:21:46 +0000")));
}
@Test
public void rssDate3Test() {
LocalDateTime dateTime = new LocalDateTime(2019, 1, 4, 22, 21, 46);
assertEquals(0, dateTime.compareTo(DateUtils.stringToLocalDateTime("Fri, 04 Jan 2019 22:21:46")));
}
@Test
public void atomJsonDateTest() {
LocalDateTime dateTime = new LocalDateTime(2019, 1, 4, 22, 21, 46);