user906153
user906153

Reputation: 1218

Convert String timestamp with offset to Java date

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

Upvotes: 3

Views: 1938

Answers (2)

James Daily
James Daily

Reputation: 597

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);

Upvotes: 2

djm.im
djm.im

Reputation: 3323

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);

Upvotes: 3

Related Questions