将长值中的 UTC 时间转换为 Android 中的 GMT 时间
Convert UTC time in long value to GMT time in Android
我的 UTC 时间长值 = 1555415100000L。
现在我想转换成不同时区的本地时间。
示例:
1555415100000L = 2019/04/16 18:45 (GMT+7)
1555415100000L = 2019/04/16 14:45(格林威治标准时间+3)
...
你有什么解决这个问题的建议吗?
这个方法是我写的。此方法 returns 特定 GMT 作为格式化字符串。您应该为此方法提供以毫秒为单位的时间和 GMT 值。
private String getSpecificGmtDate(long timeMillis, int gmt) {
long time = timeMillis + (gmt * 1000 * 60 * 60);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
return (sdf.format(new Date(time)) + " (GMT " + gmt + ")");
}
输出:
System.out.println(getSpecificGmtDate(1555415100000L, 0));
16/04/2019 11:45 (GMT 0)
System.out.println(getSpecificGmtDate(1555415100000L, 3));
16/04/2019 14:45 (GMT 3)
System.out.println(getSpecificGmtDate(1555415100000L, -3));
16/04/2019 08:45 (GMT -3)
我的 UTC 时间长值 = 1555415100000L。 现在我想转换成不同时区的本地时间。
示例:
1555415100000L = 2019/04/16 18:45 (GMT+7)
1555415100000L = 2019/04/16 14:45(格林威治标准时间+3) ...
你有什么解决这个问题的建议吗?
这个方法是我写的。此方法 returns 特定 GMT 作为格式化字符串。您应该为此方法提供以毫秒为单位的时间和 GMT 值。
private String getSpecificGmtDate(long timeMillis, int gmt) {
long time = timeMillis + (gmt * 1000 * 60 * 60);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
return (sdf.format(new Date(time)) + " (GMT " + gmt + ")");
}
输出:
System.out.println(getSpecificGmtDate(1555415100000L, 0));
16/04/2019 11:45 (GMT 0)
System.out.println(getSpecificGmtDate(1555415100000L, 3));
16/04/2019 14:45 (GMT 3)
System.out.println(getSpecificGmtDate(1555415100000L, -3));
16/04/2019 08:45 (GMT -3)