简体   繁体   English

将任何时区的日期和时间转换为 UTC 区域

[英]convert date and time in any timezone to UTC zone

  • this is my date " 15-05-2014 00:00:00 "这是我的日期“15-05-2014 00:00:00”

  • how to convert IST to UTC ie( to 14-05-2014 18:30:00)如何将 IST 转换为 UTC ie( to 14-05-2014 18:30:00)

  • based on from timezone to UTC timezone.基于从时区到 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)如果用户从任何区域输入相同的日期,则将获得 UTC 时间(例如:从澳大利亚然后 15-05-2014 00:00:00 到 14-05-2014 16:00:00)

  • please any suggestions.请提出任何建议。

You cannot "convert that date values" to other timezones or UTC.您不能“将该日期值转换为”其他时区或 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).类型java.util.Date没有任何内部时区状态,仅以用户无法更改的方式通过规范引用 UTC(仅计算自 UTC 时区中的 UNIX 纪元以来的毫秒数,不考虑闰秒)。

But you can convert the formatted String-representation of a java.util.Date to another timezone.但是您可以将java.util.Date的格式化字符串表示形式转换为另一个时区。 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):我也更喜欢在你的情况下使用“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

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 Hochschild is 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.time framework is built into Java 8 and later. java.time框架内置于 Java 8 及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat .这些类取代麻烦的老传统日期时间类,如java.util.DateCalendar ,和SimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial .要了解更多信息,请参阅Oracle 教程 And search Stack Overflow for many examples and explanations.并在 Stack Overflow 上搜索许多示例和解释。 Specification is JSR 310 .规范是JSR 310

With a JDBC driver complying with JDBC 4.2 or later, you may exchange java.time objects directly with your database.使用符合JDBC 4.2或更高版本的JDBC 驱动程序,您可以直接与您的数据库交换java.time对象。 No need for strings or java.sql.* classes.不需要字符串或 java.sql.* 类。

Where to obtain the java.time classes?从哪里获得 java.time 类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目用额外的类扩展了 java.time。 This project is a proving ground for possible future additions to java.time.该项目是未来可能添加到 java.time 的试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more .您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM