Surya Rao Rayarao
Surya Rao Rayarao

Reputation: 505

How to convert Joda LocalDate to String in Java?

I got the answer: It's very simple.

DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/yyyy");
String formattedDate = jodeLocalDateObj.toString( fmt );

Upvotes: 28

Views: 76530

Answers (3)

stephen.hanson
stephen.hanson

Reputation: 9624

LocalDate's toString can take a format string directly, so you can skip creating the DateTimeFormatter:

String formattedDate = myLocalDate.toString("MM/dd/yyyy");

https://www.joda.org/joda-time/apidocs/org/joda/time/LocalDate.html#toString-java.lang.String-

Upvotes: 96

Jon Skeet
Jon Skeet

Reputation: 1503180

While the answer you've found will work, I prefer to look at it the other way round, in terms of which object is "active" (in terms of formatting) and which is just providing data:

LocalDate localDate = new LocalDate(2010, 9, 14);
DateTimeFormatter formatter = DateTimeFormat.forPattern("MM/dd/yyyy");
String formattedDate = formatter.print(localDate);

Upvotes: 42

Surya Rao Rayarao
Surya Rao Rayarao

Reputation: 505

DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/yyyy");
String formattedDate = jodeLocalDateObj.toString( fmt );

Upvotes: 6

Related Questions