0

I have seen this post which explains how to get the number of milliseconds since 1970 for the CURRENT DATE:

how to get the number of seconds passed since 1970 for a date value?

My question is: how to get this value in milliseconds for any date ?

Thanks !!

Community
  • 1
  • 1
toto_tata
  • 14,526
  • 27
  • 108
  • 198
  • 1
    Since 1970 (I'm assuming you mean 1st January 1970) in which timezone? May sound daft, but the user's phone will give you a time in their timezone and do you want the milliseconds since midnight first January in that timezone or in GMT/UTC? – Steve Bosman Oct 04 '11 at 19:31
  • Hi ! I want the milliseconds in the same time zone as the date entered as input. Let's say that it is the time zone of the user. Thanks. – toto_tata Oct 04 '11 at 20:01

1 Answers1

4

Calendar c = new Calendar();

Calendar c = Calendar.getInstance();
// year, month, day, hourOfDay, minute
c.set(1990, 7, 12, 9, 34);
long millis = c.getTimeInMillis();
  • Thanks. I have got the rror "Cannot instantiate type Calendar". Do you know how to solve this error ? Thanks. – toto_tata Oct 04 '11 at 19:59
  • Ops sorry, that is a protected constructor. Wrote that from the top of my head. Fixed the example. –  Oct 04 '11 at 20:01
  • Thanks !! Nevertheless, I have got a kind of bug... When I write Date d = new Date(millis); after long millis = c.getTimeInMillis(); I have got a date which is offsetted of one month compared to what it should ! With the date in your example, it returns: 1990/8/12 and not 1990/7/12... How do you explain this ?? Thanks. – toto_tata Oct 04 '11 at 20:09
  • 1
    `Calendar.set()` month values range from 0 (january) to 11 (december). So this is the correct date, we set august here, not july. –  Oct 04 '11 at 20:17
  • 3
    If you don't want to get confused, you can replace the number (7 here) with a calendar month constant (`Calendar.AUGUST` here). This way it's clear and increases code readability. –  Oct 04 '11 at 20:20