Java 将任何时区的日期和时间转换为 UTC 区域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24240896/
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
convert date and time in any timezone to UTC zone
提问by user3599212
this is my date " 15-05-2014 00:00:00 "
how to convert IST to UTC i.e( to 14-05-2014 18:30:00)
- based on from timezone to UTC timezone.
这是我的日期“15-05-2014 00:00:00”
如何将 IST 转换为 UTC ie( to 14-05-2014 18:30:00)
- 基于从时区到 UTC 时区。
my code is
我的代码是
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("IST")); //here set timezone
System.out.println(formatter.format(date));
formatter.setTimeZone(TimeZone.getTimeZone("UTC")); //static UTC timezone
System.out.println(formatter.format(date));
String str = formatter.format(date);
Date date1 = formatter.parse(str);
System.out.println(date1.toString());
if user enter same date from any zone then will get UTC time(ex: from Australia then 15-05-2014 00:00:00 to 14-05-2014 16:00:00)
please any suggestions.
如果用户从任何区域输入相同的日期,则将获得 UTC 时间(例如:从澳大利亚然后 15-05-2014 00:00:00 到 14-05-2014 16:00:00)
请提出任何建议。
采纳答案by Meno Hochschild
You cannot "convert that date values" to other timezones or UTC.The type java.util.Date
does not have any internal timezone state and only refers to UTC by spec in a way which cannot be changed by user (just counting the milliseconds since UNIX epoch in UTC timezone leaving aside leapseconds).
您不能“将该日期值转换为”其他时区或 UTC。该类型java.util.Date
没有任何内部时区状态,仅以用户无法更改的方式通过规范引用 UTC(仅计算自 UTC 时区中的 UNIX 纪元以来的毫秒数,不考虑闰秒)。
But you can convert the formatted String-representation of a java.util.Date
to another timezone.I prefer to use two different formatters, one per timezone (and pattern). I also prefer to use "Asia/Kolkata" in your case because then it will universally works (IST could also be "Israel Standard Time" which will be interpreted differently in Israel):
但是您可以将 a 的格式化字符串表示形式转换java.util.Date
为另一个时区。我更喜欢使用两种不同的格式化程序,每个时区(和模式)一个。我也更喜欢在你的情况下使用“Asia/Kolkata”,因为这样它会普遍有效(IST也可以是“以色列标准时间”,在以色列会有不同的解释):
DateFormat formatterIST = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
formatterIST.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata")); // better than using IST
Date date = formatterIST.parse("15-05-2014 00:00:00");
System.out.println(formatterIST.format(date)); // output: 15-05-2014 00:00:00
DateFormat formatterUTC = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
formatterUTC.setTimeZone(TimeZone.getTimeZone("UTC")); // UTC timezone
System.out.println(formatterUTC.format(date)); // output: 14-05-2014 18:30:00
// output in system timezone using pattern "EEE MMM dd HH:mm:ss zzz yyyy"
System.out.println(date.toString()); // output in my timezone: Wed May 14 20:30:00 CEST 2014
回答by Basil Bourque
tl;dr
tl;博士
LocalDateTime.parse(
"15-05-2014 00:00:00" ,
DateTimeFormatter.ofPattern( "dd-MM-uuuu HH:mm:ss" )
)
.atZone( ZoneId.of( "Asia/Kolkata" ) )
.toInstant()
java.time
时间
The Answer by Meno Hochschildis correct but shows classes that are now outdated.
Meno Hochschild的答案是正确的,但显示的类现在已经过时了。
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu HH:mm:ss" ) ;
LocalDateTime ldt = LocalDateTime.parse( "15-05-2014 00:00:00" , f ) ;
ldt.toString(): 2014-05-15T00:00
ldt.toString(): 2014-05-15T00:00
Apparently you are certain that string represents a moment in India time. Tip: You should have included the zone or offset in that string. Even better, use standard ISO 8601 formats.
显然,您确定字符串代表印度时间的某个时刻。提示:您应该在该字符串中包含区域或偏移量。更好的是,使用标准的 ISO 8601 格式。
Assign the India time zone.
分配印度时区。
ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;
zdt.toString(): 2014-05-15T00:00+05:30[Asia/Kolkata]
zdt.toString(): 2014-05-15T00:00+05:30[亚洲/加尔各答]
To see the same moment, the same point on the timeline, through the wall-clock time of UTC, extract an Instant
.
要查看时间轴上的同一时刻、同一点,通过 UTC 的挂钟时间,提取一个Instant
.
Instant instant = zdt.toInstant() ;
instant.toString(): 2014-05-14T18:30:00Z
Instant.toString(): 2014-05-14T18:30:00Z
About java.time
关于java.time
The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date
, Calendar
, & SimpleDateFormat
。
The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
With a JDBC drivercomplying with JDBC 4.2or later, you may exchange java.timeobjects directly with your database. No need for strings or java.sql.* classes.
使用符合JDBC 4.2或更高版本的JDBC 驱动程序,您可以直接与您的数据库交换java.time对象。不需要字符串或 java.sql.* 类。
Where to obtain the java.time classes?
从哪里获得 java.time 类?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABPproject adapts ThreeTen-Backport(mentioned above). See How to use ThreeTenABP….
- Java SE 8、Java SE 9及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- java.time 类的更高版本的 Android 捆绑实现。
- 对于早期的 Android,ThreeTenABP项目采用了ThreeTen-Backport(上面提到过)。请参阅如何使用ThreeTenABP ...。
The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。