Java 8 将给定的时间和时区转换为 UTC 时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34605691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java 8 Convert given time and time zone to UTC time
提问by ttt
I have a time with string type like: "2015-01-05 17:00"
and ZoneId
is "Australia/Sydney"
.
我有一段时间使用字符串类型,例如:"2015-01-05 17:00"
and ZoneId
is "Australia/Sydney"
。
How can I convert this time information to the corresponding to UTC time using Java 8 datetime API?
如何使用 Java 8 datetime API 将此时间信息转换为对应的 UTC 时间?
Also need to considering DST stuff.
还需要考虑夏令时的东西。
采纳答案by Mateusz Sroka
You are looking for ZonedDateTime
class in Java8 - a complete date-time with time-zone and resolved offset from UTC/Greenwich. In terms of design, this class should be viewed primarily as the combination of a LocalDateTime
and a ZoneId
. The ZoneOffset
is a vital, but secondary, piece of information, used to ensure that the class represents an instant, especially during a daylight savings overlap.
您正在寻找ZonedDateTime
Java8中的类 - 一个完整的日期时间与时区和已解决的 UTC/格林威治偏移量。在设计方面,这个类应该主要被视为 aLocalDateTime
和 a的组合ZoneId
。这ZoneOffset
是一个至关重要但次要的信息,用于确保该类代表一个瞬间,尤其是在夏令时重叠期间。
For example:
例如:
ZoneId australia = ZoneId.of("Australia/Sydney");
String str = "2015-01-05 17:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime localtDateAndTime = LocalDateTime.parse(str, formatter);
ZonedDateTime dateAndTimeInSydney = ZonedDateTime.of(localtDateAndTime, australia );
System.out.println("Current date and time in a particular timezone : " + dateAndTimeInSydney);
ZonedDateTime utcDate = dateAndTimeInSydney.withZoneSameInstant(ZoneOffset.UTC);
System.out.println("Current date and time in UTC : " + utcDate);
回答by assylias
An alternative to the existing answer is to setup the formatter with the appropriate time zone:
现有答案的替代方法是使用适当的时区设置格式化程序:
String input = "2015-01-05 17:00";
ZoneId zone = ZoneId.of("Australia/Sydney");
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").withZone(zone);
ZonedDateTime utc = ZonedDateTime.parse(input, fmt).withZoneSameInstant(UTC);
Since you want to interact with a database, you may need a java.sql.Timestamp
, in which case you don't need to explicitly convert to a UTC time but can use an Instant instead:
由于您想与数据库交互,您可能需要一个java.sql.Timestamp
,在这种情况下,您不需要显式转换为 UTC 时间,而是可以使用 Instant :
ZonedDateTime zdt = ZonedDateTime.parse(input, fmt);
Timestamp sqlTs = Timestamp.from(zdt.toInstant());
回答by Mukesh
**// Refactored Logic**
ZoneId australia = ZoneId.of("Australia/Sydney");
ZoneId utcZoneID= ZoneId.of("Etc/UTC");
String ausTime = "2015-01-05 17:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
//converting in datetime of java8
LocalDateTime ausDateAndTime = LocalDateTime.parse(ausTime, formatter);
// DateTime With Zone
ZonedDateTime utcDateAndTime = ausDateAndTime.atZone(utcZoneID);
// output - 2015-01-05T17:00Z[Etc/UTC]
// With Formating DateTime
String utcDateTime = utcDateAndTime.format(formatter);
// output - 2015-01-05 17:00