-1

I use getTime() method of Date class in java.

when I perform it in my local it return an value different with when I perform it in other pc whereas date value is same.

start.getTime()
Shivkumar kondi
  • 6,458
  • 9
  • 31
  • 58
Ali_Rezvani
  • 29
  • 2
  • 4

2 Answers2

1

The Date object is effectively just a container for milliseconds-since-the-Epoch values (milliseconds since Jan 1st 1970 at midnight UTC), which is the value you get from getTime. You've said "...whereas date value is same..." which suggests you're looking at other aspects of the Date object, like getHours and such, but note all those "Deprecated" notices on those methods. They're there for a reason.

If you use something that's designed to handle timezones well (the new java.time stuff; at a pinch the old java.util.Calendar, but "well" is stretching it), you could easily have a Date object in one timezone which those mechanisms say is (for instance) 2017-02-01 at 11:06, and another in another timezone which they also say is 2017-02-01 at 11:06, but get different values from getTime. That's because of the difference in timezones.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

See javadoc for Date.getTime():

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object

Note that the String representation of the Date instance (the String returned by toString()) may differ depending on locale and time zone settings, but the getTime() method will always behave as described above.

marthursson
  • 3,242
  • 1
  • 18
  • 28