Converting a string to date format when the String ends in Z

mattt :

I have to parse the following String into a more readable date format.

String date = "20190112151605.0Z";

However, I've never encountered the Z before. I know it has to do with the time zone but when I try to use my usual code I get a java.lang.NumberFormatException.

My code is as follows:

 String whenChanged = "20190112151605.0Z";  

 long DIFF_NET_JAVA_FOR_DATE_AND_TIMES = 11644473600000L;
 long adDate1 = Long.parseLong(whenChanged);
 long adLongDate1 = ( adDate1  / 10000 ) - DIFF_NET_JAVA_FOR_DATE_AND_TIMES;
 Date lastLogonDate1 = new Date(adLongDate1);
 String format2 = new SimpleDateFormat("MM/dd/yyyy 
 HH:mma'Z'").format(lastLogonDate1);

Any help would be great. Thanks

Brad :

This will do the trick. The Z means UTC time zone

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmss.Sz");
ZonedDateTime parsed = ZonedDateTime.parse("20190112151605.0Z", fmt);

System.out.println(parsed);     // prints 2019-01-12T15:16:05

See DateTimeFormatter

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=145589&siteId=1