我正在编写一个Java代码,我能够将本地时间转换为UTC.But,当我想将它转换回我正在执行的本地time.The步骤时,我将面临这个问题-
发布于 2020-01-23 15:46:29
tl;dr
通过ZonedDateTime将ZonedDateTime.withZoneSameInstant(ZoneId)转换为不同的时区
我们不建议您在日期和时间上使用java.util.SimpleDateFormat,而且您的代码显示了部分使用java.time,这意味着您可以完全切换到java.time。要格式化,请使用java.time.format.DateTimeFormatter。
参见此示例:
public static void main(String[] args) {
// get the current instant
Instant instant = Instant.now();
// and convert it to a datetime object representing date and time in UTC
ZonedDateTime ofInstant = ZonedDateTime.ofInstant(instant, ZoneId.of("UTC"));
// print it
System.out.println("Now from Instant in UTC:\t"
+ ofInstant.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
// you can have a shorter way: current moment in your system's time zone
ZonedDateTime nowHere = ZonedDateTime.now();
// or in UTC
ZonedDateTime utcNow = ZonedDateTime.now(ZoneId.of("UTC"));
// print them both
System.out.println("Now in my (system's) time zone:\t"
+ nowHere.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
System.out.println("Now in UTC:\t\t\t"
+ utcNow.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
// then format the UTC datetime back to the one your system has
ZonedDateTime backFromUtc = utcNow.withZoneSameInstant(ZoneId.systemDefault());
// and print that one, too
System.out.println("Back from UTC:\t\t\t"
+ backFromUtc.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}输出类似于:
Now from Instant in UTC: 2020-01-23T15:44:11.921Z[UTC]
Now in my (system's) time zone: 2020-01-23T16:44:12.091+01:00[Europe/Berlin]
Now in UTC: 2020-01-23T15:44:12.091Z[UTC]
Back from UTC: 2020-01-23T16:50:52.194+01:00[Europe/Berlin]发布于 2020-01-23 18:28:55
为什么它不转换为当地时间?
出现问题的地方是:字符串myDate在UTC中,但是(1)字符串中没有UTC的指示,(2)您正在使用格式化程序sdff解析它,但是此格式化程序不使用UTC (它使用的是默认时区印度标准时间)。因此,您从解析中得到了错误的结果。
可能还有另一个误解:无论时区如何,时代都是一个特定的时间点。因此,在所有时区中,从纪元开始的毫秒都是相同的。因此,问题的第一个迹象可能是当程序打印两个不同的毫秒值时。如果毫秒值应该表示相同的时间点,那么两次都应该看到相同的值。
所有这些都表明,deHaar在另一个答案中是正确的,即您应该远离所有的DateFormat、SimpleDateFormat、Date和TimeZone。所有这些课程的设计都很糟糕,而且已经过时了。我认为您对上述类的使用对使代码变得比所需的更复杂做出了很大贡献。
编辑:
所以你告诉我不要使用DateFormat,SimpleDateFormat和TimeZone.So,你能建议我应该使用哪一门课吗?
在使用Instant和来自java.time的ZonedDateTime (现代java.time和time )时,您已经走上了很好的道路。这个API提供了您需要的所有功能,并且使用起来更好,所以我建议您坚持使用它。您可能会发现有用的其他java.time类包括ZoneId (而不是TimeZone)和DateTimeFormatter (而不是SimpleDateFormat和DateFormat)。有关更多信息,请参见教程和/或文档。稍后我会在底部添加链接。
若要将UTC字符串解析为自时代以来的毫秒,请执行以下操作:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss.SSS");
String utcString = "2020/01/22 13:22:55.731";
long millisSinceEpoch = LocalDateTime.parse(utcString, formatter)
.atOffset(ZoneOffset.UTC)
.toInstant()
.toEpochMilli();
System.out.println(millisSinceEpoch);1579699375731
或者如果你真正的目标是把它转换成印度的当地时间:
ZoneId indiaStandardTime = ZoneId.of("Asia/Kolkata");
ZonedDateTime timeInIndia = LocalDateTime.parse(utcString, formatter)
.atOffset(ZoneOffset.UTC)
.atZoneSameInstant(indiaStandardTime);
System.out.println(timeInIndia);2020年-01-22T18:52:55.731+05:30亚洲/加尔各答
我首先告诉Java在UTC中解释字符串,然后将它转换为IST。
链接
https://stackoverflow.com/questions/59882082
复制相似问题