From 6fb60c8dc48bbeb3908f146e1df310848a4e62a0 Mon Sep 17 00:00:00 2001 From: Ash Date: Sun, 27 Mar 2022 21:56:30 +0800 Subject: [PATCH] Fix parsing the publish date of RSS items --- .../ash/reader/data/repository/RssHelper.kt | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt b/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt index 9c458ee..cc7aaaf 100644 --- a/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt +++ b/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt @@ -14,6 +14,8 @@ import net.dankito.readability4j.Readability4J import net.dankito.readability4j.extended.Readability4JExtended import okhttp3.* import java.io.IOException +import java.text.ParsePosition +import java.text.SimpleDateFormat import java.util.* import javax.inject.Inject @@ -40,7 +42,13 @@ class RssHelper @Inject constructor( id = accountId.spacerDollar(UUID.randomUUID().toString()), accountId = accountId, feedId = feed.id, - date = Date(it.publishDate.toString()), + date = it.publishDate.toString().let { + try { + Date(it) + } catch (e: IllegalArgumentException) { + parseDate(it) ?: Date() + } + }, title = it.title.toString(), author = it.author, rawDescription = it.description.toString(), @@ -171,4 +179,26 @@ class RssHelper @Inject constructor( } ) } + + private fun parseDate( + inputDate: String, patterns: Array = arrayOf( + "yyyy-MM-dd", + "yyyy-MM-dd HH:mm:ss", + "yyyyMMdd", + "yyyy/MM/dd", + "yyyy年MM月dd日", + "yyyy MM dd" + ) + ): Date? { + val df = SimpleDateFormat() + for (pattern in patterns) { + df.applyPattern(pattern) + df.isLenient = false + val date = df.parse(inputDate, ParsePosition(0)) + if (date != null) { + return date + } + } + return null + } } \ No newline at end of file