53

How to get the millisecond time from date? I have the following code.

Date beginupd = new Date(cursor1.getLong(1));

This variable beginupd contains the format

Wed Oct 12 11:55:03 GMT+05:30 2011

Now how to convert this format to the millisecond time in Long datatype?

0

4 Answers 4

92
long millisecond = beginupd.getTime();

Date.getTime() JavaDoc states:

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

1
  • Wonderful! Glad to have been of help. Commented Oct 12, 2011 at 5:43
13
date.setTime(milliseconds);

this is for set milliseconds in date

long milli = date.getTime();

This is for get time in milliseconds.

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

11

You could use

Calendar cal = Calendar.getInstance();
cal.setTime(beginupd);
long millis = cal.getTimeInMillis();
3
  • 1
    Works, and Calendar is more intuitive than Date.
    – flash
    Commented Oct 12, 2011 at 5:42
  • 10
    Constructing a Calendar, setting the Date and then requesting it back using getTimeInMillis() is simpler than calling Date.getTime()? Commented Oct 12, 2011 at 5:43
  • 1
    I think @flash meant Calendar cal = Calendar.getInstance(); cal.set(2011,9,12,5,42,0); long ms = cal.getTimeInMillis(); is more intuitive than using old Date API even if his answer is less simple.
    – oHo
    Commented Jan 4, 2015 at 20:56
1

beginupd.getTime() will give you time in milliseconds since January 1, 1970, 00:00:00 GMT till the time you have specified in Date object

Your Answer

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

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