1

i want a very simple format to parse using datatimeformatter for the following:

1 1 1976 this is in d M yyyy format

What works:

val format = new SimpleDateFormat("d M yyyy")
format.parse("1 1 1976")

but

val format = DateTimeFormatter.ofPattern("d M yyyy")
LocalDateTime.parse("1 1 1976", format)

throws

'1 1 1976' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 1976-01-01 of type java.time.format.Parsed 

not sure what is the best way to use Java 8 time in my case

abroy
  • 83
  • 5
  • For future reference, before posting here please do a Google search on the error message. The first result for `"Unable to obtain LocalDateTime from TemporalAccessor"` was the existing SO post that answers your question. Also, StackOverflow's search functionality is _abysmal_, and since Google does such a good job of indexing SO, there's no pressure on the SO developers to improve search or duplicate location :-( – Jim Garrison Mar 15 '18 at 19:14

1 Answers1

2

Use LocalDate instead of LocalDateTime solve your issue.

val format = DateTimeFormatter.ofPattern("d M yyyy");
val date = LocalDate.parse("1 1 1976", format);
System.out.println(date);
Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
Amit Bera
  • 7,075
  • 1
  • 19
  • 42