0

I am getting current time from Ruby on Rails webservice in Unix Timestamp format (ie. in seconds from 1 Jan 1970), the timezone on server is UTC.

In Java I am trying to convert local current time to UTC time. But every time it is giving 6+ minutes ahead time. I want to get the difference of UTC current time and the time returned from service. My Java code is -

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
Date utc_current = new Date(System.currentTimeMillis());
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
long serverTime = 1424936268000L;
long resTime = sdf.getCalendar().getTimeInMillis() - serverTime;
System.out.println("Time Diff : " + resTime);

Where serverTime is the time I am getting from webservice. And the value for resTime shows negative value which is approx 6+ minutes.

So my question is why UTC timezone giving ahead time for System.currentTimeMillis?

halfer
  • 19,824
  • 17
  • 99
  • 186
Deepu
  • 2,590
  • 19
  • 52
  • 74
  • 1
    What does the timezone have to do with the question? Your code doesn't use utc_current, and it simply compares the current time with a hard-coded long value. I suspect the 6+ minutes is the time it takes to copy and paste the time printed by ruby in your Java code, recompile the Java code, and run it. Your code could be simplified to `System.out.println("Time Diff : " + (System.currentTimeMillis() - 1424936268000L));` – JB Nizet Feb 26 '15 at 07:59

2 Answers2

3

In contrast to the assumption in a comment of of @JB Nizet the expressions sdf.getCalendar().getTimeInMillis() and System.currentTimeMillis() are not equivalent. Proof:

SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println("date via System.currentTimeMillis()=" + f.format(utc_current));
System.out.println("date via sdf.getCalendar()=" + f.format(new Date(resTime)));

Output:

date via System.currentTimeMillis()=2015-02-26T12:19:09
date via sdf.getCalendar()=1889-12-31T04:41:21

If you carefully study the source code of SimpleDateFormat and DateFormat you will find within the initialization part code like:

private void initializeDefaultCentury() {
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add( Calendar.YEAR, -80 );
    parseAmbiguousDatesAsAfter(calendar.getTime());
}

The conclusion is to strictly avoid the method getCalendar() on your DateFormat-object. It is only used as intermediate mutable object for internal format and parse processing. It is hard to say what you will really get as time this way. Instead use directly System.currentTimeMillis() to compare your local time with server time.

Another problem is the pattern you use. "dd-MM-yyyy hh:mm:ss" is probably not correct because it uses the clock hour of half day in range 1-12 but the information for am/pm is missing. Use better the pattern symbol HH. Check the documentation of webservice for the right format.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
0

Make sure the the clock on the server and on the client machine are synchronized. The 6 minutes could simply be an offset between the two.

amahfouz
  • 2,328
  • 2
  • 16
  • 21