Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") 将时区作为 IST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19112357/
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
Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") gives timezone as IST
提问by Pradip Borde
I have SimpleDateFormat constructor as
我有 SimpleDateFormat 构造函数
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
and I am parsing string "2013-09-29T18:46:19Z".
我正在解析字符串 "2013-09-29T18:46:19Z".
I have read that here Z represents the GMT/UTC
timezone. but when I print this date on console , It prints IST timezne for the returned date.
我读过这里 Z 代表GMT/UTC
时区。但是当我在控制台上打印此日期时,它会为返回的日期打印 IST timezne。
Now my question is whether my output is right or wrong?
现在我的问题是我的输出是对还是错?
采纳答案by Peter Lawrey
You haven't set the timezone only added a Z
to the end of the date/time, so it will look like a GMT date/time but this doesn't change the value.
您尚未设置时区,只Z
在日期/时间的末尾添加了一个,因此它看起来像 GMT 日期/时间,但这不会更改该值。
Set the timezone to GMT and it will be correct.
将时区设置为 GMT,它将是正确的。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
回答by Subir Kumar Sao
'T'
and 'Z'
are considered here as constants. You need to pass Z
without the quotes. Moreover you need to specify the timezone in the input string.
'T'
并且'Z'
在此处被视为常数。你需要通过Z
没有引号。此外,您需要在输入字符串中指定时区。
Example : 2013-09-29T18:46:19-0700
And the format as "yyyy-MM-dd'T'HH:mm:ssZ"
示例:2013-09-29T18:46:19-0700
格式为"yyyy-MM-dd'T'HH:mm:ssZ"
回答by AZ_
From ISO 8601 String to Java Date Object
从 ISO 8601 字符串到 Java 日期对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
sdf.parse("2013-09-29T18:46:19Z"); //prints-> Mon Sep 30 02:46:19 CST 2013
if you don't set TimeZone.getTimeZone("GMT")
then it will output Sun Sep 29 18:46:19 CST 2013
如果你不设置,TimeZone.getTimeZone("GMT")
那么它会输出Sun Sep 29 18:46:19 CST 2013
From Java Date Object to ISO 8601 String
从 Java 日期对象到 ISO 8601 字符串
And to convert Date
object to ISO 8601 Standard (yyyy-MM-dd'T'HH:mm:ss'Z'
) use following code
并将Date
对象转换为 ISO 8601 标准 ( yyyy-MM-dd'T'HH:mm:ss'Z'
) 使用以下代码
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sdf.format(new Date())); //-prints-> 2015-01-22T03:23:26Z
Also note that without ' '
at Z yyyy-MM-dd'T'HH:mm:ssZ
prints 2015-01-22T03:41:02+0000
另请注意,没有' '
at Zyyyy-MM-dd'T'HH:mm:ssZ
打印2015-01-22T03:41:02+0000
回答by MyTitle
IF you want to handle 'standard' JSON representation of the Date then better to use this pattern: "yyyy-MM-dd'T'HH:mm:ssX"
.
如果你想处理“标准日” JSON表示,则最好使用此模式:"yyyy-MM-dd'T'HH:mm:ssX"
。
Notice the X
on the end. It will handle timezones in ISO 8601 standard, and ISO 8601 is exactly what produces this statement in Javascript new Date().toJSON()
注意X
最后。 它将处理 ISO 8601 标准中的时区,而 ISO 8601 正是在 Javascript 中生成此语句的原因new Date().toJSON()
Comparing to other answers it has some benefits:
与其他答案相比,它有一些好处:
- You don't need to require your clients to send date in GMT
- You don't need to explicitly convert your Date object to GMT using this:
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
- 您不需要要求您的客户以格林威治标准时间发送日期
- 您不需要使用以下方法将 Date 对象显式转换为 GMT:
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
回答by Basil Bourque
tl;dr
tl;博士
The other Answers are outmoded as of Java 8.
从 Java 8 开始,其他答案已经过时了。
Instant // Represent a moment in UTC.
.parse( "2013-09-29T18:46:19Z" ) // Parse text in standard ISO 8601 format where the `Z` means UTC, pronounces “Zulu”.
.atZone( // Adjust from UTC to a time zone.
ZoneId.of( "Asia/Kolkata" )
) // Returns a `ZonedDateTime` object.
ISO 8601
ISO 8601
Your string format happens to comply with the ISO 8601standard. This standard defines sensible formats for representing various date-time values as text.
您的字符串格式恰好符合ISO 8601标准。该标准定义了将各种日期时间值表示为文本的合理格式。
java.time
时间
The old java.util.Date
/.Calendar
and java.text.SimpleDateFormat
classes have been supplanted by the java.time framework built into Java 8 and later. See Tutorial. Avoid the old classesas they have proven to be poorly designed, confusing, and troublesome.
旧的java.util.Date
/.Calendar
和java.text.SimpleDateFormat
类已被 Java 8 及更高版本中内置的 java.time 框架所取代。请参阅教程。避免使用旧类,因为它们已被证明设计不佳、令人困惑且麻烦。
Part of the poor design in the old classes has bitten you, where the toString
method applies the JVM's current default time zone when generating a text representation of the date-time value that is actually in UTC (GMT); well-intentioned but confusing.
旧类中糟糕的设计的一部分让您toString
感到困扰,其中该方法在生成实际采用 UTC (GMT) 的日期时间值的文本表示时应用 JVM 的当前默认时区;善意但令人困惑。
The java.time classes use ISO 8601 formats by default when parsing/generating textual representations of date-time values. So no need to specify a parsing pattern.
java.time 类在解析/生成日期时间值的文本表示时默认使用 ISO 8601 格式。所以不需要指定解析模式。
An Instant
is a moment on the timeline in UTC.
Instant instant = Instant.parse( "2013-09-29T18:46:19Z" );
You can apply a time zoneas needed to produce a ZonedDateTime
object.
您可以根据需要应用时区来生成ZonedDateTime
对象。
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( zoneId );
回答by Thirdman
and if you don't have the option to go on java8 better use 'yyyy-MM-dd'T'HH:mm:ssXXX' as this gets correctly parsed again (while with only one X this may not be the case... depending on your parsing function)
如果您没有选择继续使用 java8,最好使用 'yyyy-MM-dd'T'HH:mm:ssX XX',因为这会再次正确解析(而只有一个 X,情况可能并非如此。 .. 取决于您的解析功能)
X generates: +01
X 生成:+01
XXX generates: +01:00
XXX 生成:+01:00
回答by niraj
For Java 8:You can use inbuilt java.time.format.DateTimeFormatter
to reduce any chance of typos,
like
对于 Java 8:您可以使用内置java.time.format.DateTimeFormatter
来减少任何拼写错误的机会,例如
DateTimeFormatter formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
ISO_ZONED_DATE_TIME represents 2011-12-03T10:15:30+01:00[Europe/Paris]
is one of the bundled standard DateTime formats provided by Oracle link
ISO_ZONED_DATE_TIME 表示2011-12-03T10:15:30+01:00[Europe/Paris]
是 Oracle链接提供的捆绑标准 DateTime 格式之一