使用SimpleDateFormat解析3个字符的缩写月份时,ParseException [英] ParseException when parsing 3 character abbreviated month using SimpleDateFormat

查看:310
本文介绍了使用SimpleDateFormat解析3个字符的缩写月份时,ParseException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,

SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date dft  = (Date) format.parse("16-MAY-2018 09:30:22:000");

我低于例外

java.text.ParseException:无法解析的日期:"16-MAY-2018 09:30:22"

java.text.ParseException: Unparseable date: "16-MAY-2018 09:30:22"

解析毫秒的方法是什么?

What's to be used to parse milliseconds?

推荐答案

该模式应为MMM,因为一个月中有三个字符.

The pattern should be MMM because there are three characters in the month.

您还应该喜欢 java.time 类(如果您使用的是Java 8或更高版本),则为当前正在使用的类:

You should also prefer java.time classes to the ones you're currently using if you're on Java 8 or above:

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
    .appendPattern("dd-MMM-yyyy")
    .appendLiteral(' ')
    .append(ISO_LOCAL_TIME)
    .toFormatter();

LocalDateTime timestamp = LocalDateTime.parse("16-May-2018 09:30:22", formatter);

这篇关于使用SimpleDateFormat解析3个字符的缩写月份时,ParseException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆