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

use*_*212 8 java timezone date simpledateformat

  • 这是我约会的日期"15-05-2014 00:00:00"

  • 如何将IST转换为UTC即(至14-05-2014 18:30:00)

  • 基于从时区到UTC时区.

我的代码是

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());
Run Code Online (Sandbox Code Playgroud)
  • 如果用户从任何区域输入相同的日期,那么将获得UTC时间(例如:从澳大利亚然后15-05-2014 00:00:00到14-05-2014 16:00:00)

  • 请任何建议.

Men*_*ild 13

您不能"将该日期值转换为"其他时区或UTC.该类型java.util.Date没有任何内部时区状态,并且仅以用户无法更改的方式引用UTC(仅计算自UTC时区中的UNIX纪元以来的毫秒数,而忽略了闰秒).

但是您可以将a的格式化String表示转换java.util.Date为另一个时区.我更喜欢使用两种不同的格式化程序,每个时区(和模式)一个.我也更喜欢在你的情况下使用"亚洲/加尔各答",因为它将普遍起作用(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
Run Code Online (Sandbox Code Playgroud)


Bas*_*que 6

tl; dr

LocalDateTime.parse( 
    "15-05-2014 00:00:00" , 
    DateTimeFormatter.ofPattern( "dd-MM-uuuu HH:mm:ss" ) 
)
.atZone( ZoneId.of( "Asia/Kolkata" ) )
.toInstant()
Run Code Online (Sandbox Code Playgroud)

java.time

Meno Hochschild答案是正确的,但显示的课程现已过时。

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu HH:mm:ss" ) ;
LocalDateTime ldt = LocalDateTime.parse( "15-05-2014 00:00:00" , f ) ;
Run Code Online (Sandbox Code Playgroud)

ldt.toString():2014-05-15T00:00

显然,您可以确定字符串代表印度时间。提示:您应该在该字符串中包含区域或偏移量。更好的是,使用标准ISO 8601格式。

分配印度时区。

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;
Run Code Online (Sandbox Code Playgroud)

zdt.toString():2014-05-15T00:00 + 05:30 [亚洲/加尔各答]

要查看时间轴上的同一时刻,同一点,通过UTC的挂钟时间,请提取Instant

Instant instant = zdt.toInstant() ;
Run Code Online (Sandbox Code Playgroud)

Instant.toString():2014-05-14T18:30:00Z


关于java.time

java.time框架是建立在Java 8和更高版本。这些类取代麻烦的老传统日期时间类,如java.util.DateCalendar,和SimpleDateFormat

现在处于维护模式Joda-Time项目建议迁移到java.time类。

要了解更多信息,请参见Oracle教程。并在Stack Overflow中搜索许多示例和说明。规格为JSR 310

使用符合JDBC 4.2或更高版本的JDBC驱动程序,您可以直接与数据库交换java.time对象。不需要字符串或java.sql。*类。

在哪里获取java.time类?

ThreeTen-额外项目与其他类扩展java.time。该项目为将来可能在java.time中添加内容提供了一个试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多