3

I need to convert this timestamp string to a Java Date object

2014-04-03T14:02:57.182+0200

How do I do this? How do I handle the time offset included in the timestamp

user906153
  • 1,218
  • 8
  • 30
  • 43
  • possible duplicate of [Converting ISO 8601-compliant String to java.util.Date](http://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date) – Basil Bourque Aug 05 '14 at 18:46

2 Answers2

3

You can use this code:

String strdate = "2014-04-03T14:02:57.182+0200";
Date date = new SimpleDateFormat("yyyy-mm-dd'T'HH:mm:ss.SSSZ").parse(strdate);
System.out.println(date);
djm.im
  • 3,295
  • 4
  • 30
  • 45
2

Thread-safe alternative from apache commons lang3. First:

import org.apache.commons.lang3.time.FastDateFormat;

then:

String strdate = "2014-04-03T14:02:57.182+0200";
String dateFormatPattern = "yyyy-mm-dd'T'HH:mm:ss.SSSZ";
Date date = FastDateFormat.getInstance(dateFormatPattern).parse(strdate);
System.out.println(date);
James Daily
  • 587
  • 6
  • 21