Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parsing date from day of week with java datetimeformatter

I'm parsing text from an sms service using a java program, and I see that when using DateTimeFormatter 'E' or 'e' are the pattern characters for days of the week, and that the number of chars determines type of format (i.e. 'E' = T, 'EE' = Tu, 'EEE' = Tue, etc...) but in my program I'm never sure what format the message will include.

If I'm trying to check for the day of the week, can I check for all the formats using a single pattern? If not, when I'm checking for fully written out days (which vary in character length) how many E's in a row will include both Monday (6 letters) and Wednesday (9 letters)?

for example:

String singleWordFromMessage = "thursday";
LocalDateTime potentialDate = LocalDateTime.parse(singleWordFromMessage, DateTimeFormatter.ofPattern("EEEE"));

I'm missing how that works in the docs and my searches here have not been fruitful so far. Thanks in advance for any help.

like image 206
Mercutio Avatar asked Aug 19 '15 18:08

Mercutio


1 Answers

As @RC pointed out, I missed:

"Text: The text style is determined based on the number of pattern letters used. Less than 4 pattern letters will use the short form. Exactly 4 pattern letters will use the full form. Exactly 5 pattern letters will use the narrow form. "

So the pattern "EEEE" would format to the day's full name, regardless of length. The patterns do not seem to support multiple types, but the same can be achieved by stripping the string to just the 'short' or 'narrow' day.

Thanks for all the help.

like image 184
Mercutio Avatar answered Sep 29 '22 16:09

Mercutio