Parse time with ZonedDateTime on android O and up

This commit is contained in:
Balazs Toldi 2023-07-30 20:04:15 +02:00
parent 3c91007d93
commit de3bea0729
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
2 changed files with 36 additions and 22 deletions

View File

@ -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;

View File

@ -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");