2

I am trying to convert GMT to IST.

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSS");
  Date c= sdf.parse("2017-03-31T10:38:14.4723017Z");

  Date date = new Date();
  DateFormat istFormat = new SimpleDateFormat();
  DateFormat gmtFormat = new SimpleDateFormat();
  TimeZone gmtTime = TimeZone.getTimeZone("GMT");
  TimeZone istTime = TimeZone.getTimeZone("IST");

  istFormat.setTimeZone(gmtTime);
  gmtFormat.setTimeZone(istTime);
  System.out.println("GMT Time: " + istFormat.format(c));
  System.out.println("IST Time: " + gmtFormat.format(c));

My output is

GMT Time: 31/3/17 6:26 AM
IST Time: 31/3/17 11:56 AM

But my actual output should be

GMT Time: 31/3/17 5:08 AM
IST Time: 31/3/17 10:38 AM

What is wrong with my code?

3 Answers 3

7

Milliseconds (SSS) can only be three digits. On more than that, the date rolls over - e.g. 10:38:14.1000 becomes 10:38:15.000. Add a couple of million milliseconds... and you get the behaviour that you're seeing now.

Try this.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
Date c = sdf.parse("2017-03-31T10:38:14.472Z");

System.out.println(c);

DateFormat istFormat = new SimpleDateFormat();
DateFormat gmtFormat = new SimpleDateFormat();
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
TimeZone istTime = TimeZone.getTimeZone("IST");

istFormat.setTimeZone(gmtTime);
gmtFormat.setTimeZone(istTime);
System.out.println("GMT Time: " + istFormat.format(c));
System.out.println("IST Time: " + gmtFormat.format(c));
4
  • In this case 4723017 comes out to 1 hour and 18 minutes. This matches the offset seen between the actual and expected. May 16, 2017 at 12:08
  • I am getting response with 7digits of ms. Is there any way to change it programmatically?
    – kushi
    May 16, 2017 at 12:14
  • 1
    It's probably not in milliseconds, it might be in nanoseconds. Do a substring to cut off the extra numbers if they are not necessary. May 16, 2017 at 12:15
  • 1
    Parse the first 3 digits. This is probably being repersented as an ISO 8601:2004 date where "fractions of Seconds" can be specified. See: stackoverflow.com/questions/25842840/…. Java thinks they are milliseconds. I ran into this problem before when passing dates between Java and .NET.
    – Allan
    May 16, 2017 at 12:16
0

Have you tried changing the format to "yyyy-MM-dd'T'HH:mm:ss.sssssssZ"?

Also, when you type Z in your Date object, you should write the time zone designator, for example, "2017-03-31T10:38:14.4723017+01:00".

1
  • 2
    's' is seconds in a minute, so that will still result in strange behaviour. The original issue comes down to the representation of the date itself - milliseconds cannot be more than three digits.
    – Riaan Nel
    May 16, 2017 at 12:09
0

Try use this format "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.