How to convert date format to milliseconds?
Asked Answered
E

4

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?

Eliza answered 12/10, 2011 at 5:35 Comment(0)
E
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.

Exurbia answered 12/10, 2011 at 5:38 Comment(1)
Wonderful! Glad to have been of help.Exurbia
C
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

Crore answered 12/10, 2011 at 5:54 Comment(0)
E
11

You could use

Calendar cal = Calendar.getInstance();
cal.setTime(beginupd);
long millis = cal.getTimeInMillis();
Ecology answered 12/10, 2011 at 5:40 Comment(3)
Works, and Calendar is more intuitive than Date.Ecology
Constructing a Calendar, setting the Date and then requesting it back using getTimeInMillis() is simpler than calling Date.getTime()?Morganstein
I think @Ecology 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.Ecclesiastes
C
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

Creolized answered 12/10, 2011 at 5:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.