在java中将字符串转换为日期 - 时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19536695/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:10:13  来源:igfitidea点击:

Convert String to Date in java - Time zone

javadate

提问by Amit Saxena

I have a String, 2013-10-07T23:59:51.205-07:00, want to convert this to Java date object. I am getting parsing error.

我有一个字符串,2013-10-07T23:59:51.205-07:00想将其转换为 Java 日期对象。我收到解析错误。

date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse("2013-10-07T23:59:51.205-07:00");

回答by Scary Wombat

try

尝试

date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
                 .parse("2013-10-07T23:59:51.205-0700");

The Z is not a literal and the timezone does not have a colon

Z 不是文字,时区没有冒号

See the examples at http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

请参阅http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html 上的示例

If java7 is being used then Zcan be replaced with Xand the timezone can have a colon

如果正在使用 java7 然后Z可以替换为X并且时区可以有一个冒号

回答by Rohit Jain

Zshouldn't be inside quotes. I don't think Zwould work for your given timezone. Before Java 7, I guess there wasn't any format to parse ISO 8601 format timezone with colon in between. You should use -0700instead.

Z不应该在引号内。我认为不适Z用于您给定的时区。在 Java 7 之前,我想没有任何格式可以解析 ISO 8601 格式时区,中间有冒号。你应该-0700改用。

However, from Java 7 onwards, you have an option for parsing ISO 8601 format timezoneusing Xinstead of Z. See javadoc for SimpleDateFormat. Just use the following format:

但是,从 Java 7 开始,您可以选择使用代替来解析ISO 8601 格式时区。有关. 只需使用以下格式:XZSimpleDateFormat

// This would work from Java 7 onwards
date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX")    
                     .parse("2013-10-07T23:59:51.205-07:00");

回答by Akkusativobjekt

The problem is that -07:00is not a valid Time zone . The Time Zone should have this format, for example something like -0800.

问题是这-07:00不是有效的时区。时区应该具有这种格式,例如类似-0800.

回答by Cirou

Your pattern is wrong, you should use the following:

您的模式是错误的,您应该使用以下内容:

date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
                        .parse("2013-10-07T23:59:51.205-07:00");                      

The 'X' indicates the Time zone in the ISO 8601 format as expressed in your Stringhere: '.205-07:00'

“X”表示 ISO 8601 格式的时区,如您String在此处表示的:“.205-07:00”

For more information read the doc: SimpleDateFormat

有关更多信息,请阅读文档:SimpleDateFormat

回答by Masudul

You should use XXXfor the format -07:00, instead of Zand X.

您应该使用XXXfor 格式-07:00,而不是Zand X

   Date sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
           .parse("2013-10-07T23:59:51.205-07:00");

Look at the example of this docs.

看看这个文档的例子。

回答by Whome

Use this trick to parse ISO8601 datetime format. I admit have not tried this with millisecond part within a string value maybe it gives you an extra headache. This works for Java6.

使用此技巧解析 ISO8601 日期时间格式。我承认没有在字符串值中使用毫秒部分尝试过这个,也许它会让你更加头疼。这适用于 Java6。

import javax.xml.bind.DatatypeConverter;
Calendar cal = DatatypeConverter.parseDateTime(strDatetime);

If am remembering correct cal instance may not use a system-default timezone. Its initialized to the origin string value timezone. If you want instance to use system timezone you can do this conversion.

如果我记得正确的 cal 实例可能不使用系统默认时区。它被初始化为原始字符串值时区。如果您希望实例使用系统时区,您可以进行此转换。

   long ts = cal.getTimeInMillis();
   cal = Calendar.getInstance();
   cal.setTimeInMillis(ts);