Java: Nicht in der Lage zu erhalten, LocalDate von TemporalAccessor

Ich versuche, ändern Sie das format einer String Datum von EEEE MMMM d zu MM/d/yyyy wird zuerst die Umwandlung in einen LocalDate und dann die Anwendung auf einen Formatierer, der ein anderes Muster der LocalDate vor der Analyse in String wieder.

Hier ist mein code:

private String convertDate(String stringDate) 
{
    //from EEEE MMMM d -> MM/dd/yyyy

    DateTimeFormatter formatter = new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .append(DateTimeFormatter.ofPattern("EEEE MMMM d"))
            .toFormatter();

    LocalDate parsedDate = LocalDate.parse(stringDate, formatter);
    DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/d/yyyy");

    String formattedStringDate = parsedDate.format(formatter2);

    return formattedStringDate;
}

Allerdings bekomme ich diese exception-Nachricht, die ich nicht wirklich verstehe:

Exception in thread "main" java.time.format.DateTimeParseException: Text 'TUESDAY JULY 25' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {DayOfWeek=2, MonthOfYear=7, DayOfMonth=25},ISO of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
  • Gut, man kann nicht ein Datum ohne Jahr.
  • Sie haben nicht gesagt, was Sie erwarten, dass das Jahr zu werden. Das aktuelle Jahr? Immer 2017? Aus der Arbeit des Jahres basierend auf der Kombination von Tag-des-Monats-und Tag-der-Woche? Entweder KayV oder Hugo am nächsten ist vom Anwendungsfall abhängig.
  • Nahezu doppelte von Java Autocomplete/Format Datum von MM-DD-Eingang
InformationsquelleAutor | 2017-07-26
Schreibe einen Kommentar