[英]Android get UTC milliseconds from GMT milliseconds
I have time variable in GMT and I will convert in UTC.我在 GMT 中有时间变量,我将在 UTC 中进行转换。 I post my code..
我发布我的代码..
long mytime = 1376824500000;
Date date = new Date(mytime);
String time = new SimpleDateFormat("HH:mm").format(date);
This return "13:15"
in some device, but I would like to have always utc date: "11:15"
.这在某些设备中返回
"13:15"
,但我希望始终使用 utc date: "11:15"
。 How can I do that?我怎样才能做到这一点? Thank you.
谢谢你。
It's not clear what difference you're expecting between UTC and GMT - for the purposes we're talking here, they're equivalent.不清楚您期望 UTC 和 GMT 之间有什么区别 - 就我们在这里讨论的目的而言,它们是等效的。 (They're not quite technically the same, but...)
(它们在技术上并不完全相同,但是......)
In terms of formatting , you just need to set the time zone on your formatter:在格式化方面,您只需要在格式化程序上设置时区:
// TODO: Consider setting a locale explicitly
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
String time = format.format(date);
Try this:试试这个:
long mytime = 1376824500000;
Date date = new Date(mytime);
SimpleDateFormat formater = = new SimpleDateFormat("HH:mm");
formater .setTimeZone(TimeZone.getTimeZone("GMT"));
String time formater.format(date);
The java.util
Date-Time API and their formatting API, SimpleDateFormat
are outdated and error-prone. java.util
Date-Time API 及其格式化 API SimpleDateFormat
已过时且容易出错。 It is recommended to stop using them completely and switch to the modern Date-Time API * .建议完全停止使用它们并切换到现代 Date-Time API * 。
Solution using java.time
, the modern Date-Time API:使用现代日期时间 API
java.time
解决方案:
import java.time.Instant;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
Instant instant = Instant.ofEpochMilli(1_376_824_500_000L);
OffsetDateTime odtUtc = instant.atOffset(ZoneOffset.UTC);
LocalTime time = odtUtc.toLocalTime();
System.out.println(time);
// If you want the time with timezone offset
OffsetTime ot = odtUtc.toOffsetTime();
System.out.println(ot);
}
}
Output:输出:
11:15
11:15Z
The Z
in the output is the timezone designator for zero-timezone offset.输出中的
Z
是零时区偏移的时区指示符。 It stands for Zulu and specifies the Etc/UTC
timezone (which has the timezone offset of +00:00
hours).它代表祖鲁语并指定
Etc/UTC
时区(时区偏移为+00:00
小时)。
Learn more about the modern Date-Time API from Trail: Date Time .从Trail: Date Time 中了解有关现代日期时间 API 的更多信息。
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project . * 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用ThreeTen-Backport,它将大部分java.time功能向后移植到 Java 6 和 7。如果您正在为 Android 项目和您的 Android API 工作级别仍然不符合 Java-8,请检查 通过 desugaring和How to use ThreeTenABP in Android Project 可用的 Java 8+ APIs 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.