简体   繁体   English

java.lang.IllegalArgumentException:解析错误:无法解析日期格式

[英]java.lang.IllegalArgumentException: Parse error: Unable to parse Date format

I need to format following date fomat 我需要格式化以下日期格式

 timeBooked: "2015-05-20T02:08:00.000Z",
 ExpiryTime: "2015-05-20T04:08:00.000Z",

My code follows to format the date: 我的代码遵循以下格式设置日期:

try {
     Date currentDate = new Date(timeBooked);
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.ZZ", Locale.ENGLISH);
     Log.e(TAG, "formatted time string: " + sdf.format(currentDate.getTime()));

     Log.e(TAG, "date string:=" + currentDate.getDate());
} catch (Exception e) {
         Log.e(TAG, e.getMessage());
         e.printStackTrace();
}

While running this code getting java.lang.IllegalArgumentException: Parse error:2015-05-20T02:08:00.000Z . 运行此代码时,获取java.lang.IllegalArgumentException:解析错误:2015-05-20T02:08:00.000Z

Your format String is incorrect. 您的格式字符串不正确。 It should be "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" . 它应该是"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

Then to get your date correctly you should use: 然后,为了正确获取日期,您应该使用:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
// Set time zone to UTC since 'Z' at end of String specifies it.
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); 
// The parsed date will be offset from UTC in device's current TimeZone.
Date currentDate = sdf.parse(timeBooked);

The Date constructor taking a string is deprecated: see http://developer.android.com/reference/java/util/Date.html#Date(java.lang.String) . 不建议使用采用字符串的Date构造函数:请参见http://developer.android.com/reference/java/util/Date.html#Date(java.lang.String) It won't accept a custom date format; 它不接受自定义日期格式; only a pre-defined set of formats are allowed. 仅允许使用一组预定义的格式。

Once you get your DateFormat string correct (I think "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" should work) you can call sdf.parse(timeBooked) to get a valid Date. 一旦您的DateFormat字符串正确(我认为“ yyyy-MM-dd'T'HH:mm:ss.SSS'Z'”应该可以工作),您可以调用sdf.parse(timeBooked)以获取有效的Date。

Z Has Meaning Z有含义

Both of the previous Answers tell you to expect-and-ignore the Z character, by surrounding with single quotes in the coded parsing pattern. 前面的两个答案都告诉您期望和忽略Z字符,方法是在编码的解析模式中用单引号引起来。 Bad advice. 不好的建议。 You would be ignoring valuable data, and would be rejecting valid alternative inputs such as 2015-05-20T02:08:00.000+05:30 . 您将忽略有价值的数据,并拒绝有效的替代输入,例如2015-05-20T02:08:00.000+05:30 The pattern code Z means "any valid offset-from-UTC ". 模式代码Z表示“任何有效的自UTC的偏移量 ”。 Adding the single quotes for 'Z' says "expect an uppercase Z to appear here, ignore any meaning it may have, and throw an exception if the Z is missing". 添加'Z'的单引号表示“期望大写Z出现在此处,忽略它可能具有的任何含义,如果Z丢失则抛出异常”。

Joda-Time 乔达时间

You are using the old date-time classes bundled with early versions of Java. 您正在使用与早期Java版本捆绑在一起的旧日期时间类。 Those classes have proven to be troublesome, flawed in both design and implementation. 这些类被证明是麻烦的,在设计和实现上都有缺陷。 Avoid them. 避免他们。 In current Android, add the Joda-Time library to your project. 在当前的Android中,将Joda-Time库添加到您的项目中。 In Java 8 and later, use the java.time framework that was inspired by Joda-Time. 在Java 8和更高版本中,请使用受Joda-Time启发的java.time框架。

Your string inputs are in ISO 8601 standard format. 您的字符串输入采用ISO 8601标准格式。 Both Joda-Time and java.time use ISO 8601 as their defaults when parsing/generating textual representations of date-time values. 解析/生成日期时间值的文本表示时,Joda-Time和java.time均使用ISO 8601作为其默认值。 So you these classes can directly parse such strings without you needing to specify any coded parsing patterns. 因此,这些类可以直接解析此类字符串,而无需指定任何编码的解析模式。

The following code creates a date-time assigned an offset-from-UTC of 0, which is what the Z (for Zulu ) means. 以下代码创建一个日期时间,该日期时间的UTC偏移量为0,这就是Z (对于Zulu )的含义。

DateTime dateTime = DateTime.parse( "2015-05-20T02:08:00.000Z" );

Using a constructor has a different meaning. 使用构造函数具有不同的含义。 The following code parses the value with an offset of zero but then adjusts the results into your JVM's current default time zone. 以下代码解析偏移量为零的值,然后将结果调整为JVM当前的默认时区。

DateTime dateTime = new DateTime( "2015-05-20T02:08:00.000Z" ) );

I suggest you always explicitly assign your desired/expected time zone. 我建议您始终明确分配所需/期望的时区。

DateTimeZone zone = DateTimezone.forID( "Europe/Paris" );
DateTime dateTime_Europe_Paris = dateTime.withZone( zone );

If you really need a java.util.Date, convert after doing your parsing and business logic with Joda-Time. 如果您确实需要java.util.Date,请在使用Joda-Time进行解析和业务逻辑后进行转换。

java.util.Date date = dateTime.toDate();

Search StackOverflow for many more Questions and Answers with example code for Joda-Time. 搜索StackOverflow上更多问题和解答与乔达时间示例代码。

暂无
暂无

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

相关问题 java.lang.IllegalArgumentException:解析错误 - 日期格式错误? - java.lang.IllegalArgumentException: Parse error - Date format error? AST 方法解析中的 java.lang.IllegalArgumentException - java.lang.IllegalArgumentException in AST method parse java.lang.IllegalArgumentException:要解析的InputStream为null - java.lang.IllegalArgumentException: InputStream to parse is null 无法启动 web 服务器; 嵌套异常是 java.lang.IllegalArgumentException:无法解析密钥库 - Unable to start web server; nested exception is java.lang.IllegalArgumentException: Failed to parse keystore java.lang.IllegalArgumentException:无法解析参数编号:电话 - java.lang.IllegalArgumentException: can't parse argument number: Phone 使用匕首刀柄 + 解析服务器 SDK: java.lang.IllegalArgumentException - Using dagger hilt + parse Server SDK: java.lang.IllegalArgumentException JBoss登录Web应用程序错误-无法解析请求。java.lang.IllegalArgumentException:查询中的非法字符 - JBoss Login Web Application error - Failed to parse request.: java.lang.IllegalArgumentException: Illegal character in query java.lang.IllegalArgumentException:无法将给定的Object格式化为Date - java.lang.IllegalArgumentException: Cannot format given Object as a Date 错误:java.lang.IllegalArgumentException - Error: java.lang.IllegalArgumentException 使用jxl错误'java.lang.IllegalArgumentException格式无效,格式错误' - Error 'java.lang.IllegalArgumentException Invalid format is malformed at' using jxl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM