简体   繁体   中英

How to convert date format to milliseconds?

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...

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.

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

You could use

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

beginupd.getTime()将给你自1970年1月1日00:00:00 GMT到你在Date对象中指定的时间以毫秒为单位的时间

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM