gpt4 book ai didi

java.lang.IllegalArgumentException : Parse error: Unable to parse Date format 异常

转载 作者:行者123 更新时间:2023-11-30 01:38:10 25 4
gpt4 key购买 nike

我需要按照日期格式格式化

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

我的代码如下格式化日期:

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();
}

运行此代码时出现 java.lang.IllegalArgumentException: Parse error:2015-05-20T02:08:00.000Z

最佳答案

您的字符串格式不正确。它应该是 "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

然后要正确获取日期,您应该使用:

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);

关于java.lang.IllegalArgumentException : Parse error: Unable to parse Date format 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34867682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com