将带有毫秒和时区的字符串转换为日期
Convert string with millisecond and timezone to Date
我有这种格式的字符串:
2017-04-06T09:29:12.225Z
我需要它的日期格式,我试过:
private Date convertStringToDate(String dateString) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sssZ");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertedDate;
}
但是它显示无法解析的错误?
尝试
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
毫秒要大写S,Z要用单引号:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Try毫秒应该大写S:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
因为 Java 7 你可以使用
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
X
代表 ISO 8601 时区,它可能只是 Z
(零时区)或类似 -08
、-0800
或 -08:00
。
另见 javadoc of SimpleDateFormat.
中给出的 table
我有这种格式的字符串: 2017-04-06T09:29:12.225Z
我需要它的日期格式,我试过:
private Date convertStringToDate(String dateString) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sssZ");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertedDate;
}
但是它显示无法解析的错误?
尝试
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
毫秒要大写S,Z要用单引号:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Try毫秒应该大写S:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
因为 Java 7 你可以使用
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
X
代表 ISO 8601 时区,它可能只是 Z
(零时区)或类似 -08
、-0800
或 -08:00
。
另见 javadoc of SimpleDateFormat.