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);
We can convert String to Date in java using parse() method of DateFormat and SimpleDateFormat classes.
Joda-Time is an API created by joda.org which offers better classes and having efficient methods to handle date and time than classes from java. util package like Calendar, Gregorian Calendar, Date, etc. This API is included in Java 8.0 with the java.
Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat. Low level - builder.
So the short answer to your question is: YES (deprecated).
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With