使用Java的UTC本地时间 [英] Local Time to UTC in Java
问题描述
我正在尝试在Java中将本地时间转换为UTC时间。我已经编写了代码:
I am trying to convert local time to UTC time in java. I have written the code:
String datesToConvert = "31-12-2018 23:37:00";
String dateFormat = "dd-MM-yyyy HH:mm:ss";
SimpleDateFormat sdf2 = new SimpleDateFormat(dateFormat);
sdf2.setTimeZone(TimeZone.getDefault());
Date gmt = null;
SimpleDateFormat sdfOutPutToSend = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdfOutPutToSend.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
gmt = sdf2.parse(datesToConvert);
System.out.println("UTC FORMATTED DATE : " + sdfOutPutToSend.format(gmt));
} catch (Exception e) {
e.printStackTrace();
}
我得到的输出是:
UTC格式日期:2018 -12-31T18:07:00.000Z
The Output I am getting is : UTC FORMATTED DATE : 2018-12-31T18:07:00.000Z
但是我希望它是相同的日期和时间,但是最后以.000Z进行更改,这显示了时差。
But I want it to be same date and time but changes in .000Z at last which shows the time difference.
推荐答案
tl; dr
使用现代的 java.time classes。
tl;dr
Use modern java.time classes.
LocalDateTime
.parse(
"31-12-2018 23:37:00" ,
DateTimeFormatter.ofPattern( "dd-MM-uuuu HH:mm:ss" )
)
.atOffset(
ZoneOffset.UTC
)
.toString()
2018-12-31T23:37Z
2018-12-31T23:37Z
java.time
使用现代的 java.time 类,而不是那些可怕的旧类,例如 Date
。
java.time
Use the modern java.time classes, not those terrible legacy classes such as Date
.
将输入字符串解析为 LocalDateTime
,因为它缺少任何时区或与UTC的偏移量,因此 not 不能代表
Parse your input string as a LocalDateTime
as it lacks any indicator of time zone or offset-from-UTC, and therefore does not represent a moment.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu HH:mm:ss" ) ;
LocalDateTime ldt = LocalDateTime.parse( "31-12-2018 23:37:00" , f ) ;
您声称知道用于此输入字符串的时区。如果确定,则应用 ZoneId
来获取 ZonedDateTime
,以确定实际时刻,即时间轴上的一点。
You claim to know the time zone which was intended for this input string. If you are certain, apply a ZoneId
to get a ZonedDateTime
, to determine an actual moment, a point on the timeline.
指定正确的时区名称格式为大陆/地区
,例如 美国/蒙特利尔
, 非洲/卡萨布兰卡
,或太平洋/奥克兰
。请勿使用2-4个字母的缩写,例如 EST
或 IST
,因为它们不是 真实时区,不是标准化的,甚至不是唯一的(!)。
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 2-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;
您的问题尚不清楚。但是看来您可能是在说输入内容旨在表示UTC时刻。如果是这样,请应用 ZoneOffset.UTC
常量以获取 OffsetDateTime
对象。
Your Question is not clear. But it appears you may be saying the input was intended to represent a moment in UTC. If so, apply ZoneOffset.UTC
constant to get a OffsetDateTime
object.
OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;
所需输出:2018-12-31T23:37:00.808Z
Desired Output: 2018-12-31T23:37:00.808Z
这是不可能的,因为您的输入字符串缺少小数秒 .808
。
This is impossible, as your input string lacked the fractional second .808
.
您报告的问题结尾为 .000Z
是正确的,因为零表示没有小数秒。最后的 Z
表示UTC(相对于UTC的偏移量为零),是 +00:00
,发音为 Zulu
。
The ending you reported as a problem, .000Z
would be correct as the zeros mean there is no fractional second. The Z
on the end means UTC (an offset-from-UTC of zero), is short for +00:00
, and is pronounced Zulu
.
java.time 框架内置于Java 8及更高版本中。这些类取代了麻烦的旧旧版日期时间类,例如 java.util.Date
, Calendar
,& SimpleDateFormat
。
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
Joda-Time 项目,现在处于维护模式 ,建议迁移到 java.time 类。
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
要了解更多信息,请参见 Oracle教程 。并在Stack Overflow中搜索许多示例和说明。规范为 JSR 310 。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
您可以直接与数据库交换 java.time 对象。使用符合 java.sql。*
类。
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
在哪里获取java.time类?
Where to obtain the java.time classes?
- Java SE 8 , Java SE 9 , Java SE 10 , Java SE 11 ,以及更高版本-具有捆绑实现的标准Java API。
- Java 9添加了一些次要功能和修复。
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- 大多数 java.time 功能都反向移植到Java 6&
nofollow noreferrer> ThreeTen-Backport 。
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- java.time 类的更高版本的Android捆绑实现。
- 对于较早的Android(< 26), ThreeTenABP 项目采用了> ThreeTen-返回端口 (如上所述)。请参阅 如何使用ThreeTenABP… 。
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
ThreeTen-Extra 项目使用其他类扩展了java.time。该项目为将来可能在java.time中添加内容打下了基础。您可能会在这里找到一些有用的类,例如
时间间隔
,YearWeek
,YearQuarter
和更多。这篇关于使用Java的UTC本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!