Better since time on Community Pages

This commit is contained in:
Bazsalanszky 2023-08-04 14:34:30 +02:00
parent 1bb1d3b9af
commit 9149ee224c

View File

@ -8,7 +8,12 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import eu.toldi.infinityforlemmy.utils.JSONUtils;
@ -47,7 +52,11 @@ public class ParseSubredditData {
}
boolean removed = community.getBoolean("removed");
String published = community.getString("published");
String published_raw = community.getString("published");
String published = formatISOTime(published_raw);
if(published == null){
published = published_raw;
}
String updated = "";
if (!community.isNull("updated")) {
updated = community.getString("updated");
@ -170,4 +179,25 @@ public class ParseSubredditData {
}
}
}
public static String formatISOTime(String isoTime) {
// Truncate the time to millisecond precision
String truncatedTime = isoTime.substring(0, 23);
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US);
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
Date date = isoFormat.parse(truncatedTime);
// Set your desired output format here
SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss", Locale.US);
outputFormat.setTimeZone(TimeZone.getDefault()); // Set to device's default timezone
return outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
}