[英]How to Convert a Date time value with UTC offset into GMT in java 7
I have a date time value 2016-12-21T07:48:36 with an offset of UTC+14. 我的日期时间值为2016-12-21T07:48:36,偏移量为UTC + 14。 How to convert the datetime into equivalent standard GMT time.
如何将日期时间转换为等效的标准GMT时间。
I tried with sampleDateFormat.parse()
method.But, I am not able to get the TimeZone object for UTC offset like. 我尝试使用
sampleDateFormat.parse()
,我无法获得UTC偏移量的TimeZone对象。
sampleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC+14:00"))
Please help me to convert the UTC datetime into standard GMT time in Java 7. 请帮我在Java 7中将UTC日期时间转换为标准GMT时间。
I will assume you have the original date as a string. 我假设您将原始日期作为字符串。 Do the following:
请执行下列操作:
SimpleDateFormat
and set the timezone to "GMT+14" SimpleDateFormat
并将时区设置为“GMT + 14” Date
object Date
对象 SimpleDateFormat
to "UTC" (or use a different SimpleDateFormat
instance) SimpleDateFormat
的时区设置为“UTC”(或使用不同的SimpleDateFormat
实例) Example: 例:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class ConvertToUTC {
public static void main(String[] args) throws Exception {
String dateval = "2016-12-21T07:48:36";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("GMT+14"));
Date date = df.parse(dateval);
System.out.println(df.format(date)); // GMT+14
df.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(df.format(date)); // UTC
}
}
use "GMT+14:00" instead of "UTC+14:00" 使用“GMT + 14:00”而不是“UTC + 14:00”
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("GMT+14:00"));
final Date d = f.parse("2016-12-21T07:48:36");
f.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(f.format(d)); // -> 2016-12-20T05:48:36
LocalDateTime.parse( "2016-12-21T07:48:36" ) // Parse as a `LocalDateTime` given the lack of an offset or zone. *Not* an actual moment, only a rough approximation of potential moments along a range of about 26-27 hours.
.atOffset( ZoneOffset.ofHours( 14 ) ) // Assign an offset-from-UTC as context, giving meaning to determine an actual point on the timeline.
.toInstant() // Renders `Instant` object in UTC.
The modern way is with the java.time classes built into Java 8 and later. 现代方法是使用Java 8及更高版本内置的java.time类。 Much of the functionality is back-ported to Java 6 & 7 in the ThreeTen-Backport project.
许多功能都在ThreeTen-Backport项目中反向移植到Java 6和7。
ZoneOffset offset = ZoneOffset.ofHours( 14 ); // fourteen hours ahead of UTC.
Parse the string as a LocalDateTime
as it lacks any info about offset or zone. 将字符串解析为
LocalDateTime
因为它缺少有关偏移量或区域的任何信息。 Your input is in standard ISO 8601 format, so no need to specify a formatting pattern. 您的输入采用标准ISO 8601格式,因此无需指定格式设置模式。
LocalDateTime ldt = LocalDateTime.parse( "2016-12-21T07:48:36" );
Apply the offset to the local date-time to get an OffsetDateTime
an object. 将偏移量应用于本地日期时间以获取
OffsetDateTime
对象。
OffsetDateTime odt = ldt.atOffset( offset );
From that, extract an Instant
which is always in UTC . 从中,提取始终为UTC的
Instant
。
Instant instant = odt.toInstant();
instant.toString(): 2016-12-20T17:48:36Z
instant.toString():2016-12-20T17:48:36Z
In UTC , the value is a different date, the 20th instead of 21st. 在UTC中 ,值是不同的日期,即20日而不是21日。
See live code at IdeOne.com . 查看IdeOne.com上的实时代码 。
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.Date
, Calendar
和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 。
Using a JDBC driver compliant 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 nor 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 . 您可以在这里找到一些有用的类,比如
Interval
, YearWeek
, YearQuarter
,和更多 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.