-1

I am having issues converting a String that has this format for date from the server into a long?

Example Date String - "2016-07-04T00:02:34.457Z" (Note this is a string)

I tried this below but needs try catch around gmt, when I add it and a not null around cmtDt - then I initialize cmtDt to 0 pre setting it on the bottom and it is always 0.

Calendar c = Calendar.getInstance();
TimeZone tz = c.getTimeZone();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
formatter.setTimeZone(tz);
Date gmt = formatter.parse(comment.getDateCommented());
cmtDt = gmt.getTime();
Jason
  • 11,744
  • 3
  • 42
  • 46
Lion789
  • 4,402
  • 12
  • 58
  • 96

3 Answers3

3

First, your input String includes milliseconds (and your format does not). Second, your input String includes a literal Z (which is presumably to indicate a UTC timezone). Finally, getting your system timezone and assigning it to the formatter isn't reliably going to be UTC. You need something like,

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
    Date gmt = formatter.parse("2016-07-04T00:02:34.457Z");
    long cmtDt = gmt.getTime();
    System.out.println(cmtDt);
} catch (Exception e) {
    e.printStackTrace();
}

Which I ran, and got

1467590554457
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I tried this code, getting a 0 now, @Jason's code above worked just not the timezone aspect – Lion789 Aug 11 '16 at 03:45
  • @Lion789 Note that my code (if you copied and pasted then tsk-tsk) makes `cmtDt` local to the try block (and thus it would shadow any other `cmtDt` you have). Finally, try a debugger. – Elliott Frisch Aug 11 '16 at 03:47
  • Local? I have cmtDt set outside of it if that should not work then? – Lion789 Aug 11 '16 at 03:50
  • And my code has `long cmtDt = gmt.getTime();`, which is another variable named `cmtDt`. Edit your post into a [MCVE](http://stackoverflow.com/help/mcve). – Elliott Frisch Aug 11 '16 at 03:51
  • However thank you I have combined your timezone method with the Formatting @jason provided and it works now! – Lion789 Aug 11 '16 at 03:51
1

Your format string for the SimpleDateFormat needs to be:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");

My test code that works is:

Calendar c = Calendar.getInstance();
TimeZone tz = c.getTimeZone();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
formatter.setTimeZone(tz);
Date gmt = formatter.parse("2016-07-04T00:02:34.457Z");
long cmtDt = gmt.getTime();
System.out.println("cmtDt = " + cmtDt);
Jason
  • 11,744
  • 3
  • 42
  • 46
  • Timezone does not seem to work, trying to have it locally affect and it is showing a future date... am I adding that wrong then – Lion789 Aug 11 '16 at 03:40
0

The format in SDF needs to be fixed. The following will help you.

    Calendar c = Calendar.getInstance();
    TimeZone tz = c.getTimeZone();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
    formatter.setTimeZone(tz);
    Date gmt = formatter.parse("2016-07-04T00:02:34.457Z");
    long cmtDt = gmt.getTime();
    System.out.println(cmtDt);

Prints : 1467599640457

Ramachandran.A.G
  • 4,788
  • 1
  • 12
  • 24