如何在 Java 中将 Joda LocalDate 转换为 String?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3709083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert Joda LocalDate to String in Java?
提问by Surya Rao Rayarao
I got the answer: It's very simple.
我得到了答案:这很简单。
DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/yyyy");
String formattedDate = jodeLocalDateObj.toString( fmt );
回答by Surya Rao Rayarao
DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/yyyy");
String formattedDate = jodeLocalDateObj.toString( fmt );
回答by Jon Skeet
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);
回答by stephen.hanson
LocalDate's toString
can take a format string directly, so you can skip creating the DateTimeFormatter:
LocalDatetoString
可以直接采用格式字符串,因此您可以跳过创建 DateTimeFormatter:
String formattedDate = myLocalDate.toString("MM/dd/yyyy");
https://www.joda.org/joda-time/apidocs/org/joda/time/LocalDate.html#toString-java.lang.String-
https://www.joda.org/joda-time/apidocs/org/joda/time/LocalDate.html#toString-java.lang.String-