开发者

How to convert Joda LocalDate to String in Java?

开发者 https://www.devze.com 2023-01-15 21:56 出处:网络
I got the answer: It\'s very simp开发者_Python百科le. DateTimeFormatter fmt = DateTimeFormat.forPattern(\"MM/dd/yyyy\");

I got the answer: It's very simp开发者_Python百科le.

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


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-


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);


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号