UTC 格式的日期 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20238280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Date in to UTC format Java
提问by MadTech
I have a string like this 2013-10-22T01:37:56
. I Need to change this string into UTC Date format like this MM/dd/yyyy KK:mm:ss a
. I have tried some code but it is not returning the UTC datetime.
我有一个这样的字符串2013-10-22T01:37:56
。我需要将此字符串更改为这样的 UTC 日期格式MM/dd/yyyy KK:mm:ss a
。我尝试了一些代码,但它没有返回 UTC 日期时间。
My code is
我的代码是
String[] time = itsAlarmDttm.split("T");
String aFormatDate = time[0]+ " "+time[1];
String aRevisedDate = null;
try {
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final Date dateObj = sdf.parse(aFormatDate);
aRevisedDate = new SimpleDateFormat("MM/dd/yyyy KK:mm:ss a").format(dateObj);
System.out.println(aRevisedDate);
} catch (ParseException e) {
itsLogger.error("Error occured in Parsing the Data Time Object: " +e.getMessage());
} catch (Exception e) {
itsLogger.error("Error occured in Data Time Objecct: " +e.getMessage());
}
I am getting the output is MM/dd/yyyy KK:mm:ss a
format. But Not UTC time format.
How to solve this issue?
我得到的输出是MM/dd/yyyy KK:mm:ss a
格式。但不是UTC时间格式。如何解决这个问题?
采纳答案by benjamin.d
Try to format your date with the Z
or z
timezone flags:
尝试使用Z
或z
时区标志格式化您的日期:
new SimpleDateFormat("MM/dd/yyyy KK:mm:ss a Z").format(dateObj);
回答by Buddha
Try this... Worked for me and printed 10/22/2013 01:37:56 AM
Ofcourse this is your code only with little modifications.
试试这个... 为我工作并打印10/22/2013 01:37:56 AM
当然,这是您的代码,只需稍作修改。
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); // This line converts the given date into UTC time zone
final java.util.Date dateObj = sdf.parse("2013-10-22T01:37:56");
aRevisedDate = new SimpleDateFormat("MM/dd/yyyy KK:mm:ss a").format(dateObj);
System.out.println(aRevisedDate);
回答by stan
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
// or SimpleDateFormat sdf = new SimpleDateFormat( "MM/dd/yyyy KK:mm:ss a Z" );
sdf.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
System.out.println( sdf.format( new Date() ) );
回答by Basil Bourque
What Time Zones?
什么时区?
No where in your question do you mention time zone. What time zone is implied that input string? What time zone do you want for your output? And, UTCis a time zone (or lack thereof depending on your mindset) not a string format.
您在问题中没有提到time zone。输入字符串隐含的时区是什么?您希望输出的时区是什么?而且,UTC是一个时区(或缺少时区,取决于您的心态)而不是字符串格式。
ISO 8601
ISO 8601
Your input string is in ISO 8601format, except that it lacks an offset from UTC.
您的输入字符串采用ISO 8601格式,只是它缺少与 UTC的偏移量。
Joda-Time
乔达时间
Here is some example code in Joda-Time2.3 to show you how to handle time zones. Joda-Time has built-in default formattersfor parsing and generating String representations of date-time values.
以下是Joda-Time2.3 中的一些示例代码,向您展示如何处理时区。Joda-Time 具有内置的默认格式化程序,用于解析和生成日期时间值的字符串表示。
String input = "2013-10-22T01:37:56";
DateTime dateTimeUtc = new DateTime( input, DateTimeZone.UTC );
DateTime dateTimeMontréal = dateTimeUtc.withZone( DateTimeZone.forID( "America/Montreal" );
String output = dateTimeMontréal.toString();
As for generating string representations in other formats, search StackOverflow for "Joda format".
至于生成其他格式的字符串表示,在 StackOverflow 中搜索“Joda 格式”。
回答by Ole V.V.
java.time
时间
It's about time someone provides the modern answer. The modern solution uses java.time, the modern Java date and time API. The classes SimpleDateFormat
and Date
used in the question and in a couple of the other answers are poorly designed and long outdated, the former in particular notoriously troublesome. TimeZone
is poorly designed to. I recommend you avoid those.
是时候有人提供现代答案了。现代解决方案使用 java.time,现代 Java 日期和时间 API。问题和其他几个答案中使用的类SimpleDateFormat
和类Date
设计不良且已过时,尤其是前者特别麻烦。TimeZone
设计得很差。我建议你避免这些。
ZoneId utc = ZoneId.of("Etc/UTC");
DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern(
"MM/dd/yyyy hh:mm:ss a zzz", Locale.ENGLISH);
String itsAlarmDttm = "2013-10-22T01:37:56";
ZonedDateTime utcDateTime = LocalDateTime.parse(itsAlarmDttm)
.atZone(ZoneId.systemDefault())
.withZoneSameInstant(utc);
String formatterUtcDateTime = utcDateTime.format(targetFormatter);
System.out.println(formatterUtcDateTime);
When running in my time zone, Europe/Copenhagen, the output is:
在我的时区 Europe/Copenhagen 运行时,输出为:
10/21/2013 11:37:56 PM UTC
10/21/2013 11:37:56 PM UTC
I have assumed that the string you got was in the default time zone of your JVM, a fragile assumption since that default setting can be changed at any time from another part of your program or another programming running in the same JVM. If you can, instead specify time zone explicitly, for example ZoneId.of("Europe/Podgorica")
or ZoneId.of("Asia/Kolkata")
.
我假设您得到的字符串在 JVM 的默认时区中,这是一个脆弱的假设,因为该默认设置可以随时从程序的其他部分或在同一 JVM 中运行的其他程序更改。如果可以,请改为明确指定时区,例如ZoneId.of("Europe/Podgorica")
或ZoneId.of("Asia/Kolkata")
。
I am exploiting the fact that you string is in ISO 8601 format, the format the the modern classes parse as their default, that is, without any explicit formatter.
我正在利用这样一个事实,即您的字符串采用 ISO 8601 格式,现代类将其解析为默认格式,即没有任何显式格式化程序。
I am using a ZonedDateTime
for the result date-time because it allows us to format it with UTC
in the formatted string to eliminate any and all doubt. For other purposes one would typically have wanted an OffsetDateTime
or an Instant
instead.
我使用 aZonedDateTime
作为结果日期时间,因为它允许我们使用UTC
格式化字符串对其进行格式化以消除任何和所有疑问。出于其他目的,人们通常会想要 anOffsetDateTime
或 anInstant
来代替。
Links
链接
- Oracle tutorial: Date Timeexplaining how to use java.time.
- Wikipedia article: ISO 8601
- Oracle 教程:解释如何使用 java.time 的日期时间。
- 维基百科文章:ISO 8601