From b1b12aba317d16a4524dad6fe54128158daf98c1 Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Sat, 29 Jul 2023 20:04:22 +0200 Subject: [PATCH] Fix comment/post dates on older versions of android Closes #20 --- .../comment/ParseComment.java | 24 ++++++++++++----- .../infinityforlemmy/post/ParsePost.java | 27 ++++++++++++++----- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java b/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java index 37878ad8..16386468 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java @@ -9,13 +9,15 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import java.time.ZoneId; -import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; +import java.util.TimeZone; import java.util.concurrent.Executor; import java.util.regex.Pattern; @@ -278,9 +280,19 @@ public class ParseComment { String authorQualifiedName = LemmyUtils.actorID2FullName(creatorObj.getString("actor_id")); String linkAuthor = creatorObj.getString("actor_id"); long commentTimeMillis = 0; - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - commentTimeMillis = ZonedDateTime.parse(commentObj.getString("published"), - DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("Z"))).toInstant().toEpochMilli(); + + String dateStr = commentObj.getString("published"); + + dateStr = dateStr.substring(0, dateStr.lastIndexOf(".") + 4) + 'Z'; + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault()); + sdf.setTimeZone(TimeZone.getTimeZone("UTC")); + try { + Date date = sdf.parse(dateStr); + if (date != null) { + commentTimeMillis = date.getTime(); + } + } catch (ParseException e) { + e.printStackTrace(); } String commentMarkdown = commentObj.getString("content"); String commentRawText = commentObj.getString("content"); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/post/ParsePost.java b/app/src/main/java/eu/toldi/infinityforlemmy/post/ParsePost.java index 24221751..19a2fd1b 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/post/ParsePost.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/post/ParsePost.java @@ -18,13 +18,15 @@ import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; -import java.time.ZoneId; -import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; +import java.util.Locale; +import java.util.TimeZone; import java.util.concurrent.Executor; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -151,16 +153,27 @@ public class ParsePost { String author = creator.getString("name"); String authorFull = LemmyUtils.actorID2FullName(creator.getString("actor_id")); long postTimeMillis = 0; - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - postTimeMillis = ZonedDateTime.parse(post.getString("published"), - DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("Z"))).toInstant().toEpochMilli(); + + String dateStr = post.getString("published"); + + dateStr = dateStr.substring(0, dateStr.lastIndexOf(".") + 4) + 'Z'; + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault()); + sdf.setTimeZone(TimeZone.getTimeZone("UTC")); + try { + Date date = sdf.parse(dateStr); + if (date != null) { + postTimeMillis = date.getTime(); + } + } catch (ParseException e) { + e.printStackTrace(); } + String title = post.getString("name"); String permalink = post.getString("ap_id"); int score = counts.getInt("score"); int voteType = 0; int nComments = counts.getInt("comments"); - int upvoteRatio = 100 * counts.getInt("upvotes") / max(counts.getInt("upvotes") + counts.getInt("downvotes"),1); + int upvoteRatio = 100 * counts.getInt("upvotes") / max(counts.getInt("upvotes") + counts.getInt("downvotes"), 1); boolean hidden = community.getBoolean("hidden"); boolean nsfw = post.getBoolean("nsfw"); boolean locked = post.getBoolean("locked");