I had help setting up a function to take two strings and merge them into a date object. Java - Take two strings and combine into a single date object
This has been working fine until it tries to parse 1st June, then i get the below error
Exception in thread "AWT-EventQueue-0" java.time.format.DateTimeParseException: Text '1st June' could not be parsed, unparsed text found at index 7
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1952)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)
at Timetable.ClassManager.parseDate(ClassManager.java:201)
at Timetable.GoogleAPI.loadClasses(GoogleAPI.java:133)
at Timetable.ClassManager.loadClasses(ClassManager.java:58)
The code for the function is
public LocalDateTime parseDate(String strDate, String strTime) {
strTime = strTime + ":00";
System.out.println("Date: " + strDate);
System.out.println("Time: " + strTime);
LocalDate date = LocalDate.now();
DateTimeFormatter dtfForDate = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.parseDefaulting(ChronoField.YEAR, date.getYear())
.appendPattern("d['th']['st']['rd']['nd'] MMM")
.toFormatter(Locale.ENGLISH);
DateTimeFormatter dtfForTime = DateTimeFormatter.ofPattern("H:m:s", Locale.ENGLISH);
LocalDateTime ldt = LocalDate.parse(strDate, dtfForDate)
.atTime(LocalTime.parse(strTime, dtfForTime));
System.out.println("Local Date Time: " + ldt);
return ldt;
}
The two prints give me
Date: 1st June
Time: 9:15:00
I would need to be able to handle both full month names and month abbreviations, i.e., March was set as Mar, April as Apr.