简体   繁体   English

Android上的SimpleDateFormat行为异常

[英]Weird behavior with SimpleDateFormat on Android

I'm trying to parse this value 2013-10-22T16:21:22Z using the following pattern yyyy-MM-dd'T'HH:mm:ssZ and it throws this exception : 我正在尝试使用以下模式yyyy-MM-dd'T'HH:mm:ssZ解析此值2013-10-22T16:21:22Z ,它将引发此异常:

java.text.ParseException: Unparseable date: "2013-10-22T16:21:22Z" (at offset 19)

Does anyone have an idea why? 有谁知道为什么?

I don't think it can parse the Z. Have you tried something like this? 我认为它不能解析Z。您是否尝试过这种方法?

String formattedTime = myTime.replace("Z", "+00:00");

And then tried parsing that? 然后尝试解析?

In a nutshell, throw away the legacy java way to handle date and times. 简而言之,不要使用传统的Java方法来处理日期和时间。 Instead, take a look at the Joda API site . 相反,请查看Joda API 网站 In their site you'll find a few code examples about the most common usages related to date and time manipulation, and probably some dealing with your question. 在他们的网站上,您将找到一些代码示例,这些示例涉及与日期和时间操作有关的最常见用法,并且可能还会处理一些问题。

simply, 'Z' is reserved keyword for datetime parsing. 简单来说,“ Z”是用于日期时间解析的保留关键字。 you can escape it like this: 您可以这样逃脱它:

yyyy-MM-dd'T'HH:mm:ss'Z'

If you want to know what Z means. 如果您想知道Z的含义。 please visit here. 请访问这里。 http://developer.android.com/reference/java/text/SimpleDateFormat.html http://developer.android.com/reference/java/text/SimpleDateFormat.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM