From de3bea0729897b1cc2d39e46d92a3fed52f6ccbc Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Sun, 30 Jul 2023 20:04:15 +0200 Subject: [PATCH] Parse time with ZonedDateTime on android O and up --- .../comment/ParseComment.java | 29 ++++++++++++------- .../infinityforlemmy/post/ParsePost.java | 29 ++++++++++++------- 2 files changed, 36 insertions(+), 22 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 f99a0842..fd3337d1 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/comment/ParseComment.java @@ -11,6 +11,9 @@ import org.json.JSONObject; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -283,19 +286,23 @@ 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(); + } else { + String dateStr = commentObj.getString("published"); - 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(); + 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(); } - } catch (ParseException e) { - e.printStackTrace(); } String content = MarkdownUtils.processImageCaptions(commentObj.getString("content"), "Image"); String commentMarkdown = 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 7e1c614f..7088d095 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/post/ParsePost.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/post/ParsePost.java @@ -20,6 +20,9 @@ import java.net.HttpURLConnection; import java.net.URL; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Date; import java.util.HashSet; @@ -154,19 +157,23 @@ 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(); + } else { + String dateStr = post.getString("published"); - 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(); + 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(); } - } catch (ParseException e) { - e.printStackTrace(); } String title = post.getString("name");