Util: Fix time zone for formatted time/date

This commit is contained in:
zzz
2019-09-07 11:00:29 +00:00
parent aed6d433c8
commit 8eda9abab7

View File

@ -39,6 +39,7 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.zip.Deflater; import java.util.zip.Deflater;
@ -114,10 +115,7 @@ public class DataHelper {
*/ */
private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.MEDIUM); private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.MEDIUM);
private static final DateFormat TIME_FORMAT = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT); private static final DateFormat TIME_FORMAT = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
static { private static boolean _date_tz_set, _time_tz_set;
DATE_FORMAT.setTimeZone(SystemVersion.getSystemTimeZone());
TIME_FORMAT.setTimeZone(SystemVersion.getSystemTimeZone());
}
/** Read a mapping from the stream, as defined by the I2P data structure spec, /** Read a mapping from the stream, as defined by the I2P data structure spec,
* and store it into a Properties object. * and store it into a Properties object.
@ -526,7 +524,7 @@ public class DataHelper {
fos = new SecureFileOutputStream(tmpFile); fos = new SecureFileOutputStream(tmpFile);
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"))); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(fos, "UTF-8")));
out.println("# NOTE: This I2P config file must use UTF-8 encoding"); out.println("# NOTE: This I2P config file must use UTF-8 encoding");
out.println("# Last saved: " + new Date(System.currentTimeMillis())); out.println("# Last saved: " + formatTime(System.currentTimeMillis()));
for (Map.Entry<Object, Object> entry : props.entrySet()) { for (Map.Entry<Object, Object> entry : props.entrySet()) {
String name = (String) entry.getKey(); String name = (String) entry.getKey();
String val = (String) entry.getValue(); String val = (String) entry.getValue();
@ -1631,6 +1629,12 @@ public class DataHelper {
*/ */
public static String formatDate(long now) { public static String formatDate(long now) {
synchronized(DATE_FORMAT) { synchronized(DATE_FORMAT) {
if (!_date_tz_set) {
// delayed set, too early if done in static block
TimeZone tz = SystemVersion.getSystemTimeZone();
DATE_FORMAT.setTimeZone(tz);
_date_tz_set = true;
}
return DATE_FORMAT.format(new Date(now)); return DATE_FORMAT.format(new Date(now));
} }
} }
@ -1645,6 +1649,12 @@ public class DataHelper {
*/ */
public static String formatTime(long now) { public static String formatTime(long now) {
synchronized(TIME_FORMAT) { synchronized(TIME_FORMAT) {
if (!_time_tz_set) {
// delayed set, too early if done in static block
TimeZone tz = SystemVersion.getSystemTimeZone();
TIME_FORMAT.setTimeZone(tz);
_time_tz_set = true;
}
return TIME_FORMAT.format(new Date(now)); return TIME_FORMAT.format(new Date(now));
} }
} }