问题

请注意,我不希望毫米来自时代。我想要当前时钟的毫秒数。

例如,我有这段代码。

Date date2 = new Date(); 
Long time2 = (long) (((((date2.getHours() * 60) + date2.getMinutes())* 60 ) + date2.getSeconds()) * 1000);

有没有办法获得日期的毫秒?还有另一种方法吗?

注释:System.currentTimeMillis()给了我一个不是我正在寻找的时代。


#1 热门回答(178 赞)

你的意思是?

long millis = System.currentTimeMillis() % 1000;

BTW Windows不允许时间旅行到1969年

C:\> date
Enter the new date: (dd-mm-yy) 2/8/1969
The system cannot accept the date entered.

#2 热门回答(24 赞)

使用日历

Calendar.getInstance().get(Calendar.MILLISECOND);

要么

Calendar c=Calendar.getInstance();
c.setTime(new Date()); /* whatever*/
//c.setTimeZone(...); if necessary
c.get(Calendar.MILLISECOND);

在实践中虽然我认为它几乎总是等于System.currentTimeMillis()%1000;除非某人有闰秒或某个日历定义了一个不在第二个边界上的纪元。


#3 热门回答(16 赞)

Calendar.getInstance().get(Calendar.MILLISECOND);

原文链接