Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Time with variable day

Tags:

java

java-8

I can't seem to get my date with variable whitespace to parse.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss yyyy");
LocalDateTime dateTime = LocalDateTime.parse(date, formatter);

This is the format I have to accept

Sat Jul  2 08:52:13 2016
Sat Jul 12 08:52:13 2016

I can get the top to pass if I change my formatter to the below, but it will break with a 2 digit number

EEE MMM  d HH:mm:ss yyyy
like image 568
Pumphouse Avatar asked Jul 03 '16 05:07

Pumphouse


1 Answers

You can try pattern of "EEE MMM ppd HH:mm:ss yyyy" for your format.

ppd outputs the day padded on the left with spaces to a width of 2.

See Pad modifier in DateTimeFormatter for more details.

like image 78
Wilson Avatar answered Oct 09 '22 11:10

Wilson