简体   繁体   中英

How to convert string to datetime in java using joda time

I am currently working on converting from date to string. After that I convert that string to datetime. But it error. Anyone can help me?

Here is the code.

import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;


SimpleDateFormat outFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String dt1 = outFormat.format(date1);


DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(dt1);

You're doing entirely too much work. Joda Time can convert for you in its parse(String, DateTimeFormatter) method.

DateTime dateTime = DateTime.parse(dt1, formatter);

Alternatively, if your string were in ISO8601 format (that is, yyyy-MM-dd'T'HH:mm:ssZ ), you could just use parse(String) instead:

DateTime dateTime = DateTime.parse(dt1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM