[英]How to convert Joda-Time DateTime to java.util.Date and vice versa?
[英]how to convert joda datetime to String and vice versa
有没有办法将Joda DateTime转换为String,然后从该String转换为DateTime。
DateTime d = ..;
String date = d.toString();
DateTime dateTime = DateTime.parse(date);
我的问题是上述方法是否有效或需要使用格式化程序。
试试这个
DateTime dt = new DateTime();
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
String dtStr = fmt.print(dt);
对于那些想要解析toString
值的人可以阅读我的答案。
如果你仔细阅读DateTime
的实现。 toString
方法被AbstractInstant
覆盖。
@ToString
public String toString() {
return ISODateTimeFormat.dateTime().print(this);
}
现在,解析toString
的值:
DateTime dt = new DateTime();
String dtStr = dt.toString();
DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
DateTime newDt = fmt.parse(dtStr);
LocalDate localDate = new LocalDate(2010, 9, 14);
DateTimeFormatter formatter = DateTimeFormat.forPattern("MM/dd/yyyy");
String formattedDate = formatter.print(localDate);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.