How to convert date format to milliseconds?

JavaAndroid

Java Problem Overview


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? Any help is highly appreciated and thanks in advance...

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

You could use

Calendar cal = Calendar.getInstance();
cal.setTime(beginupd);
long millis = cal.getTimeInMillis();

Solution 3 - Java

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

Solution 4 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPattabi RamanView Question on Stackoverflow
Solution 1 - JavaBuhake SindiView Answer on Stackoverflow
Solution 2 - JavaflashView Answer on Stackoverflow
Solution 3 - JavaDharmendraView Answer on Stackoverflow
Solution 4 - JavaSaurabhView Answer on Stackoverflow