6

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.

Mercutio
  • 1,152
  • 1
  • 14
  • 33
  • 3
    You misread "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. " [source](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) –  Aug 19 '15 at 18:45
  • For your unknown format, you could probably "strip" the day of the week from then string then parse. –  Aug 19 '15 at 18:48
  • Thanks so much! That's exactly what I needed – Mercutio Aug 19 '15 at 18:50
  • 1
    @JamesLingo You can write up an Answer to your own Question, and a few days later mark that Answer as accepted. This signals the question is close/resolved. Also easier for others to read rather than traipsing through comments. – Basil Bourque Aug 19 '15 at 19:11
  • Will do. @BasilBourque – Mercutio Aug 19 '15 at 20:01

1 Answers1

6

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.

Mercutio
  • 1,152
  • 1
  • 14
  • 33