How to convert this unix temporary string to java date
I got this time string "2015-07-16T03: 58: 24.932031Z", I need to convert to java timestamp, I use the following code, it seems the converted date is wrong?
public static void main(String[] args) throws ParseException {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
Date date = format.parse("2015-07-16T03:58:24.932031Z");
System.out.println("date: " + date);
System.out.println("timestamp: " + date.getTime());
}
output:
date: Thu Jul 16 04:13:56 CST 2015
timestamp: 1436991236031
Is the date format incorrect?
Thanks in advance!
source to share
You don't want to specify Z, this is the time zone indicator. Use the format specifier X
for the ISO-8601 time zone instead .
Individually, you may need to preprocess the string because the,, portion at the end .932031
is not milliseconds (remember, only 1000ms per second). Looking at this value, it could be microseconds (millionths of a second). SimpleDateFormat
has no format specifier for microseconds. You can just use regex or other string manipulation to remove the last three digits to turn it into milliseconds.
This (which assumes you've done this cropping) works: Live Copy
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
// Note --------------------------------------------------------^^^^
Date date = format.parse("2015-07-16T03:58:24.932Z");
// Note trimming --------------------------------^
System.out.println("date: " + date);
System.out.println("timestamp: " + date.getTime());
source to share