6

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);
chemat92
  • 501
  • 2
  • 7
  • 16
  • possible duplicate of [Java string to date conversion](http://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – Basil Bourque Oct 25 '14 at 16:30

1 Answers1

16

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);
tobiasbayer
  • 10,269
  • 4
  • 46
  • 64
Makoto
  • 104,088
  • 27
  • 192
  • 230