1
    String str = "Jan 15";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd");
    LocalDate parsed = LocalDate.parse(str, formatter);

I keep getting this exception:

Exception in thread "main" java.time.format.DateTimeParseException: Text 'Jan 15' could not be parsed at index 0 at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947) at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849) at java.time.LocalDate.parse(LocalDate.java:400) at com.mach5.T.main(T.java:17)

Could someone help me out?

lospejos
  • 1,976
  • 3
  • 19
  • 35
Qi Hang
  • 11
  • 1
  • 2
  • https://stackoverflow.com/questions/36113530/java-convert-string-date-to-month-name-year-mmm-yyyy – whatamidoingwithmylife Dec 02 '17 at 20:10
  • @FairPlay No, [your link](https://stackoverflow.com/questions/36113530/java-convert-string-date-to-month-name-year-mmm-yyyy) is not a duplicate. That other Question is about *generating* a year-month string from a full year-month-day value, while this Question is about *parsing* a month-day value without any year. I believe this Question here is indeed a duplicate, but I have not yet found a true original. – Basil Bourque Dec 02 '17 at 21:02

4 Answers4

2

tl;dr

Parse as a MonthDay rather than LocalDate.

MonthDay.parse( 
    "Jan 15" , 
    DateTimeFormatter.ofPattern( "MMM dd" , Locale.US )
)

--01-15

MonthDay

Your input lacks a year, so it cannot be parsed as a LocalDate. Instead, parse as a MonthDay.

If you are parsing a string that represent a month-day, use a class that represents month-day values. That class would be MonthDay.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMM dd" , Locale.US ) ;
String input =  "Jan 15" ;
MonthDay md = MonthDay.parse( input , f ) ;

md.toString(): --01-15

That double-hyphen in the generated output string follows the ISO 8601 standard, indicating the year is unknown/omitted.

LocalDate

You can assign a year if known.

LocalDate ld = md.atYear( 2017 ) ;

ld.toString(): 2017-01-15

See this code run live at IdeOne.com.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

You can do the following:

    DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                                .appendPattern("MMM dd")
                                .parseDefaulting(ChronoField.YEAR, Year.now(ZoneId.systemDefault()).getValue())
                                .toFormatter();
    LocalDate parsed = LocalDate.parse(str, formatter);
Gal Shaboodi
  • 744
  • 1
  • 7
  • 25
1

You cannot create a LocalDate without a year.

So either you need to add one.

        String str = "Jan 15 2016";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d yyyy", Locale.ENGLISH);
        LocalDate parsed = LocalDate.parse(str, formatter);

Or you can modify the Formatter to fill in the current year, you can check this post.

    String str = "Jan 15";
    DateTimeFormatter formatter = new DateTimeFormatterBuilder()
            .appendPattern("MMM d")
            .parseDefaulting(ChronoField.YEAR, Year.now(ZoneId.systemDefault()).getValue())
            .toFormatter(Locale.ENGLISH);;
    LocalDate parsed = LocalDate.parse(str, formatter);
Beri
  • 11,470
  • 4
  • 35
  • 57
1

As stated here, you are missing the year so as to be able to parse a LocaleDate.

Java: Unable to obtain LocalDate from TemporalAccessor

Example: http://tpcg.io/dC93ex

Juan
  • 5,525
  • 2
  • 15
  • 26