3

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

mattt
  • 45
  • 5
  • 1
    Here's an explanation of the Z: https://stackoverflow.com/questions/27840670/what-is-the-z-ending-on-date-strings-like-2014-01-01t000000-588z. Using that, you'll need to write logic to strip off the Z before parsing the rest of the String as a number. – Colm Bhandal Feb 11 '19 at 20:12
  • 1
    check this: https://stackoverflow.com/questions/2580925/simpledateformat-parsing-date-with-z-literal – rockfarkas Feb 11 '19 at 20:13
  • Why dont you parse your `whenChanged` string using format like this: `SimpleDateFormat("yyyyMMddHHmmss.S'Z'")`? – tsolakp Feb 11 '19 at 20:45
  • 1
    You should use the [new Date and Time API](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html). – MC Emperor Feb 11 '19 at 20:52

2 Answers2

3

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

Brad
  • 15,186
  • 11
  • 60
  • 74
  • Elegant, and thanks for showing the use of java.time, the modern Java date and time API. While lowercase `z` (time zone name) may work for parsing the `Z`, it’s customary to use uppercase `X` (zone-offset 'Z' for zero) as in `uuuuMMddHHmmss.SX`. – Ole V.V. Feb 12 '19 at 05:23
  • Thanks, yes I see that `X` works in this example as well. The OP should decide whether to use `z` or `X` depending on whether other date time strings in the data set is using either a timezone name (e.g. PST) or a timezone offset (e.g. +01:00) as described in the linked docs for DateTimeFormatter – Brad Feb 13 '19 at 11:33
0

Is it necessary for you to do anything with these time zones?

If not you could do

if(whenChanged.contains('Z')){
   whenChanged = whenChanged.substring(0,date.indexOf('Z'));
}
M. Goodman
  • 141
  • 9
  • 1
    Why would you remove fractional seconds, when it's only the Zulu time zone that needs to be removed? – Andreas Feb 11 '19 at 20:28
  • I thought the period was denoting the start of the time zone designation. I'll edit to reflect that. Thanks! – M. Goodman Feb 11 '19 at 20:30
  • `Z` means an offset of zero from UTC and needs to be parsed as such, or you will get incorrect times (on most computers). – Ole V.V. Feb 12 '19 at 05:24