简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM