Fix comment/post dates on older versions of android

Closes #20
This commit is contained in:
Balazs Toldi 2023-07-29 20:04:22 +02:00
parent 20c317b63f
commit b1b12aba31
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
2 changed files with 38 additions and 13 deletions

View File

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

View File

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