1

I understand that Date.getTime() in Java will return the millis since EPOCH, considering the Date as a UTC Date.

On Java 6, I have a Date, on which if I execute Date.getTime(), it returns the millis since EPOCH, considering the Date is in PDT timezone. (The server on which I am running the code is configured in the PDT time.)

I want the program to consider it a UTC date and return the milli seconds since EPOCH.

Following is my code snippet and the output:

 logger.debug("Date: " + someDate);
 logger.debug("someDate in millis): " + someDate.getTime());

 Output:                                                                                                         
 Date: 2016-08-19 12:04:56.993
 someDate in millis: 1471633496993 //This is time since EPOCH for 2016-08-19 12:04:56.993 PDT

whereas I want it to return the millis as 1471608296993 (1471608296993 is millis since EPOCH for UTC Date: 2016-08-19 12:04:56.993)

In short I want to get the millis since EPOCH, irrespective of the local timezone, which in my case is PDT.

Please help.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
user2306856
  • 81
  • 15

3 Answers3

2

As per Java Doc getTime()

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT

It does not use the local time zone

ravthiru
  • 8,878
  • 2
  • 43
  • 52
  • Yes, that's what I have read everywhere. But if you see the output I am getting, it doesn't seem to be the case. – user2306856 Aug 19 '16 at 12:37
  • It appears that the Date object in question is taking the supplied String and treating it as the time in OP's default timezone and behind the scenes converting it to UTC time. 12:04 PDT is not the same as 12:04 UTC, in fact it is 19:04 UTC, hence the difference in expected and actual output. – jonhopkins Aug 19 '16 at 12:40
  • use the generated milliseconds and verify here http://www.epochconverter.com/ – ravthiru Aug 19 '16 at 12:42
  • That's where I got 19:04 from. The question is valid. The Date object is treating the String as PDT, not UTC – jonhopkins Aug 19 '16 at 12:43
1

Additional to what was already said by johnhopkins.

A Date object store the time in milliseconds since January 1, 1970, 00:00:00 GMT. But when you print it out, it will be printed as date for you current time zone.

The following snippet demonstrate it.

Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.set(Calendar.YEAR, 2016);
cal.set(Calendar.MONTH, Calendar.AUGUST);
cal.set(Calendar.DAY_OF_MONTH, 19);
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 4);
cal.set(Calendar.SECOND, 56);
cal.set(Calendar.MILLISECOND, 993);

long epochMillis = cal.getTime().getTime();
System.out.println("EPOCH millis    = " + epochMillis);
System.out.println("date from cal   = " + cal.getTime());
Date date = new Date(epochMillis);
System.out.println("date from epoch = " + date);

output

EPOCH millis    = 1471608296993
date from cal   = Fri Aug 19 14:04:56 CEST 2016
date from epoch = Fri Aug 19 14:04:56 CEST 2016

The two lines date from ... print the date 2016-08-19 12:04:56.993 UTC using your default timezone.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69
0

Using the information from this answer to this question, I was able to come up with code that will correctly parse your String as a datetime in UTC. The trick is to use a SimpleDateFormat object to parse the String before creating the Date object:

String strDate = "2016-08-19 12:04:56.993";
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = isoFormat.parse(strDate);
System.out.println(date);
System.out.println(date.getTime());

Output:

Fri Aug 19 08:04:56 EDT 2016
1471608296993
Community
  • 1
  • 1
jonhopkins
  • 3,844
  • 3
  • 27
  • 39
  • Thank you jonhopkins. This helped in resolving the issue. I had tried the above mentioned solution before posting the question, but guess I was concentrating more on the Date format displayed as the output, that I didn't realize that the millis are correct. But this helped me understand that Date displayed in System.out.println uses the local timezone to display results. – user2306856 Aug 19 '16 at 15:09