Parsing into YearMonth

I would like to parse two separate strings "1982" and "SEP" into a java.time.YearMonth object.

java.time.YearMonth.parse("1978 SEP", java.time.format.DateTimeFormatter.ofPattern("yyyy LLL"))

gives me

java.time.format.DateTimeParseException: Text '1978 SEP' could not be parsed at index 5
  at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
  at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
  at java.time.YearMonth.parse(YearMonth.java:295)
Answer:1

There are 3 (possibly 2) problems here:

  • Your locale isn't English, so "SEP" can't be understood as September. This can be fixed by setting the English locale to the formatter.
  • DateTimeFormatter is case sensitive by default so you need to build a case insensitive formatter.
  • You shouldn't use the "L" token but "M" instead: refer to this question.

The following will work:

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                                    .parseCaseInsensitive()
                                    .appendPattern("yyyy MMM")
                                    .toFormatter(Locale.ENGLISH);
System.out.println(YearMonth.parse("1978 SEP", formatter));
Answer:2

I compiled my program and I got the following error. How should I resolve it? Note: ClientThreadClients.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.

We are developing a Java application to be run on Linux environments (Ubuntu for now) which communicates with a piece of hardware. Said hardware uses a .so library made available by the manufacturer ...

I have some terrible beaviour. I have the following Maven configuration: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:...

Latest project I used java8 , found it's very convenient to process Collection type, it gave me great surprise. But suddenly feel some conventional syntax are so cumbersome, for example I have an ...