2016-02-25 96 views
-1

我的问题是,我想将当地时间转换为GMT。我能转换的实际时间,并将其保存到解析,但问题是,解析应用自己:不能将时间转换为格林威治标准时间

周三2月24日10:00:00 GMT + 05:30 2016

这是我转换为GMT00:00后得到的日期,但问题是GMT+05:30实际上是错误的,因为我的日期实际上是GMT。现在,当我把这个日期放在服务器中时,它会进一步减少5:30的时间。那么我们如何才能将GMT+05:30更改为GMT+00:00

+2

你的代码在哪里? – f1sh

+0

请显示你的代码,看看你到目前为止 – NiallMitch14

+0

我认为这是这个线程在这里的重复:http://stackoverflow.com/questions/2055407/conversion-of-local-time-zone-to-gmt -in-java –

回答

1

我们将从当前的当地时间开始。

Calendar cal = Calendar.getInstance(); // automatically produces and instance of the current date/time 

据我了解,当地日期精确到GMT但你时间是5:30提前实际GMT

为了从当地时间减去5:30,您可以执行以下操作。

cal.add(Calendar.HOUR, -5); 
cal.add(Calendar.MINUTE, -30); 

System.out.println(cal.getTime()); 

相反,如果你想添加时间字段,你可以简单地使用:

cal.add(Calendar.HOUR, 5); // note I have removed the minus(-) symbol 
cal.add(Calendar.MINUTE, 30); // note I have removed the minus(-) symbol 

System.out.println(cal.getTime()); 
1

如果您使用的是Java 8.你可以从新时库使用。 当你告诉它似乎你想要什么,是在UTC邮票是GMT + 00

更多时间库here

这条线:

System.out.println(Instant.now().toString()); 

2016-02-25T17:54:55.420Z

希望它让事情对你更清楚。

+0

不需要抵消。 ['Instant'](https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html)按照UTC定义。 'Instant.now()。的toString()' –

相关问题