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 this string to datetime. But this is a mistake. Can anybody 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); 
+5
source share
1 answer

You do too much work. Joda Time can convert for you into its parse(String, DateTimeFormatter) method.

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

Alternatively, if your string was in ISO8601 format (i.e. yyyy-MM-dd'T'HH:mm:ssZ ), you can simply use parse(String) instead:

 DateTime dateTime = DateTime.parse(dt1); 
+10
source

Source: https://habr.com/ru/post/1205505/


All Articles