Java – Joda-time from the LocalDateTime string to LocalDate

Joda-time from the LocalDateTime string to LocalDate… here is a solution to the problem.

Joda-time from the LocalDateTime string to LocalDate

I’m using JodaTime to get the date and time the account was created. The format is

2017-04-05T12:38:35.585

When I got it, I

stored it as a string in my database, so I looked around for ways to format it from string to LocalDate, but anything I found online didn’t work out. In my opinion, my next step is a horrible solution, looping through the string until it finds T and removes everything behind it. So I only have left

2017-04-05. 

But ideally, set the date to if possible

05/04/2017

Solution

Use ISODateTimeFormat to get LocalDateTime and get LocalDate from it.
Take care to use the correct locale

String input="2017-04-05T12:38:35.585";

LocalDateTime ldt = ISODateTimeFormat.localDateOptionalTimeParser()
                    .withLocale(Locale.ENGLISH)
                    .parseLocalDateTime(input);

System.out.println(ldt.toLocalDate());//prints 2017-04-05

Related Problems and Solutions