Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

java - ParseException when parsing 3 character abbreviated month using SimpleDateFormat

Here is my code,

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

I am getting below exception

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

What's to be used to parse milliseconds?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...