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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:01:37  来源:igfitidea点击:

convert date and time in any timezone to UTC zone

javadatetimezonesimpledateformat

提问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.Datedoes 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.Dateto 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 类?

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 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多