Migrate to java.time
This commit is contained in:
1 parent
3ca0189f28
commit
086c0d9f28
4 files changed
+66
-175
No files matched your search
@@ -16,18 +16,17 @@
|
||||
|
||||
package com.hippo.util;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
|
||||
import com.hippo.ehviewer.R;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.time.Instant;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Locale;
|
||||
|
||||
@SuppressLint("SimpleDateFormat")
|
||||
public final class ReadableTime {
|
||||
|
||||
public static final long SECOND_MILLIS = 1000;
|
||||
@@ -51,38 +50,19 @@ public final class ReadableTime {
|
||||
R.plurals.minute,
|
||||
R.plurals.second
|
||||
};
|
||||
private static final Calendar sCalendar = Calendar.getInstance();
|
||||
private static final Object sCalendarLock = new Object();
|
||||
private static final SimpleDateFormat DATE_FORMAT_WITHOUT_YEAR = new SimpleDateFormat("MMM d");
|
||||
private static final SimpleDateFormat DATE_FORMAT_WITH_YEAR = new SimpleDateFormat("MMM d, yyyy");
|
||||
private static final SimpleDateFormat DATE_FORMAT_WITHOUT_YEAR_ZH = new SimpleDateFormat("M月d日");
|
||||
private static final SimpleDateFormat DATE_FORMAT_WITH_YEAR_ZH = new SimpleDateFormat("yyyy年M月d日");
|
||||
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yy-MM-dd HH:mm");
|
||||
private static final Object sDateFormatLock1 = new Object();
|
||||
private static final SimpleDateFormat FILENAMABLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS");
|
||||
private static final Object sDateFormatLock2 = new Object();
|
||||
private static final DateTimeFormatter DATE_FORMAT_WITHOUT_YEAR = DateTimeFormatter.ofPattern("MMM d");
|
||||
private static final DateTimeFormatter DATE_FORMAT_WITH_YEAR = DateTimeFormatter.ofPattern("MMM d, yyyy");
|
||||
private static final DateTimeFormatter DATE_FORMAT_WITHOUT_YEAR_ZH = DateTimeFormatter.ofPattern("M月d日");
|
||||
private static final DateTimeFormatter DATE_FORMAT_WITH_YEAR_ZH = DateTimeFormatter.ofPattern("yyyy年M月d日");
|
||||
private static final DateTimeFormatter FILENAMABLE_DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss-SSS");
|
||||
private static final Object sDateFormatLock = new Object();
|
||||
private static Resources sResources;
|
||||
|
||||
public static void initialize(Context context) {
|
||||
sResources = context.getApplicationContext().getResources();
|
||||
}
|
||||
|
||||
/*
|
||||
public static String getDisplayTime(long time) {
|
||||
if (Settings.getPrettyTime()) {
|
||||
return getTimeAgo(time);
|
||||
} else {
|
||||
return getPlainTime(time);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
public static String getPlainTime(long time) {
|
||||
synchronized (sDateFormatLock1) {
|
||||
return DATE_FORMAT.format(new Date(time));
|
||||
}
|
||||
}
|
||||
|
||||
public static String getTimeAgo(long time) {
|
||||
Resources resources = sResources;
|
||||
|
||||
@@ -111,49 +91,23 @@ public final class ReadableTime {
|
||||
return resources.getString(R.string.some_days_ago, days);
|
||||
} else {
|
||||
synchronized (sCalendarLock) {
|
||||
Date nowDate = new Date(now);
|
||||
Date timeDate = new Date(time);
|
||||
sCalendar.setTime(nowDate);
|
||||
int nowYear = sCalendar.get(Calendar.YEAR);
|
||||
sCalendar.setTime(timeDate);
|
||||
int timeYear = sCalendar.get(Calendar.YEAR);
|
||||
OffsetDateTime nowDate = Instant.ofEpochMilli(now).atOffset(ZoneOffset.UTC);
|
||||
OffsetDateTime timeDate = Instant.ofEpochMilli(time).atOffset(ZoneOffset.UTC);
|
||||
int nowYear = nowDate.getYear();
|
||||
int timeYear = timeDate.getYear();
|
||||
boolean isZh = Locale.getDefault().getLanguage().equals("zh");
|
||||
|
||||
if (nowYear == timeYear) {
|
||||
return (isZh ? DATE_FORMAT_WITHOUT_YEAR_ZH : DATE_FORMAT_WITHOUT_YEAR).format(timeDate);
|
||||
return (isZh ? DATE_FORMAT_WITHOUT_YEAR_ZH : DATE_FORMAT_WITHOUT_YEAR)
|
||||
.format(timeDate.toInstant().atOffset(ZoneOffset.UTC));
|
||||
} else {
|
||||
return (isZh ? DATE_FORMAT_WITH_YEAR_ZH : DATE_FORMAT_WITH_YEAR).format(timeDate);
|
||||
return (isZh ? DATE_FORMAT_WITH_YEAR_ZH : DATE_FORMAT_WITH_YEAR)
|
||||
.format(timeDate.toInstant().atOffset(ZoneOffset.UTC));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getTimeInterval(long time) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Resources resources = sResources;
|
||||
|
||||
long leftover = time;
|
||||
boolean start = false;
|
||||
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
long multiple = MULTIPLES[i];
|
||||
long quotient = leftover / multiple;
|
||||
long remainder = leftover % multiple;
|
||||
if (start || quotient != 0 || i == SIZE - 1) {
|
||||
if (start) {
|
||||
sb.append(" ");
|
||||
}
|
||||
sb.append(quotient)
|
||||
.append(" ")
|
||||
.append(resources.getQuantityString(UNITS[i], (int) quotient));
|
||||
start = true;
|
||||
}
|
||||
leftover = remainder;
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String getShortTimeInterval(long time) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Resources resources = sResources;
|
||||
@@ -173,8 +127,8 @@ public final class ReadableTime {
|
||||
}
|
||||
|
||||
public static String getFilenamableTime(long time) {
|
||||
synchronized (sDateFormatLock2) {
|
||||
return FILENAMABLE_DATE_FORMAT.format(new Date(time));
|
||||
synchronized (sDateFormatLock) {
|
||||
return FILENAMABLE_DATE_FORMAT.format(Instant.ofEpochMilli(time));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user