Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.Date get milliseconds

Tags:

java

date

time

I have done the following:

String standardRange = "00:01:01";
SimpleDateFormat rangeFormatter = new SimpleDateFormat("hh:mm:ss");
Date range = rangeFormatter.parse(standardRange);

Now:

range.getTime();

.. I get the output of -3539000 and not 61,000

I'm not sure what I'm doing wrong; when debugging, cdate exists, the attribute contains a fraction, which contains the value 61,000, which is what I want.

like image 689
Eddie Texas Avatar asked Mar 01 '13 15:03

Eddie Texas


People also ask

Does Java Util Date have milliseconds?

The java. util. Date class represents a particular moment in time, with millisecond precision since the 1st of January 1970 00:00:00 GMT (the epoch time).

How do you find milliseconds from a Date?

Javascript date getMilliseconds() method returns the milliseconds in the specified date according to local time. The value returned by getMilliseconds() is a number between 0 and 999.

How do you calculate milliseconds in Java?

currentTimeMillis() method returns the current time in milliseconds.


1 Answers

The reason you're seeing this is that the date you're creating is actually in the past of the date epoch, not 1m1s after it:

String standartRange = "00:01:01";
SimpleDateFormat rangeFormatter = new SimpleDateFormat("hh:mm:ss");
Date range = rangeFormatter.parse(standartRange);

System.out.println(new Date(0L));
System.out.println(new Date(0L).getTime());
System.out.println(range);
System.out.println(range.getTime());

and its output;

Thu Jan 01 01:00:00 GMT 1970
0
Thu Jan 01 00:01:01 GMT 1970
-3539000

The epoch date is incorrect here - it should be 00:00:00, but due to a historical bug where BST/GMT changed dates and timezone cant keep track. It seems that Sun/Oracle consider this a historical "inaccuracy".

Check out the bug report - its describes the problem more fully.

From your language (German) this may not be directly due to this BST issue, but its almost certainly related.

like image 197
Sean Landsman Avatar answered Oct 05 '22 03:10

Sean Landsman