Java Joda-Time – How to Convert DateTime to String and Vice Versa

datetimejava

Is there a way to convert Joda DateTime to String and then from that String to DateTime.

DateTime d = ..;
String date = d.toString();
DateTime dateTime = DateTime.parse(date);

My question is that whether the above approach is valid or need to use formatter.

Best Answer

Try this

DateTime dt = new DateTime();
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
String dtStr = fmt.print(dt);
Related Question