Java.util.Date get milliseconds

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 -3539000, not 61 000

I'm not sure what I'm doing wrong; when debugging cdate there is an attribute containing fraction that contains the value 61,000. This is what I want.

+6
source share
6 answers

The reason you see this is because the date you create is actually in the past era of the date, 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 his way out;

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

The date number is incorrect here - it should be 00:00:00, but due to a historical error , where the BST / GMT date has changed and the time zone cannot be tracked. It seems that Sun / Oracle consider this historic "inaccuracy."

Check the error report - it describes the problem in more detail.

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

+3
source

A Java date is not intended to calculate the length of a given time period. The getTime () call returns the number of milliseconds since January 1, 1970, 00:00:00 GMT. In your case, you are actually ending the date that precedes this era (thus a negative number). When I run my code, I get 21661000. (See Answer from Sean Landsman , for I believe he hit on why you get negative results ... hint: my number is exactly 6 hours from GMT or 21600000ms)

Joda-Time is a library that is well suited to solve your main problem.

 PeriodFormatter formatter = new PeriodFormatterBuilder() .appendHours() .appendSeparator(":") .appendMinutes() .appendSeparator(":") .appendSeconds() .toFormatter(); Period period = formatter.parsePeriod("00:01:01"); assert period.toStandardDuration().getMillis() == 61000 
+1
source

To get what you want, you must compare the time you want with the origin of time. using the code below:

 String standardRange = "00:01:01"; SimpleDateFormat rangeFormatter = new SimpleDateFormat("HH:mm:ss"); Date range = rangeFormatter.parse(standardRange); Date range2 = rangeFormatter.parse("00:00:00"); System.out.println(range.getTime() - range2.getTime()); 
0
source

According to JavaDoc, getTime() :

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT, represented by this Date object.

You want the number of milliseconds in one minute and one second.

 (60*minutes+seconds)*1000 

It really does not need to come from a Date object.

If you need to calculate the time in milliseconds for a certain interval, perhaps use the joda time library or get the day, hour, minute, second and millisecond components from your date object and calculate the value manually.

0
source

Try:

 Date range1 = rangeFormatter.parse("00:01:01"); Date range2 = rangeFormatter.parse("00:00:00"); System.out.println(range1.getTime() - range2.getTime()); 
0
source

hh:mm:ss stands for 12-hour time, which always means β€œtime point,” not β€œtime interval.” Thus, the time zone will affect the value. However, in GMT +0, the value is equal to that which represents the "time interval".

All you just need:

 rangeFormatter.setTimeZone(TimeZone.getTimeZone("GMT")); 

Give it a try!

0
source

All Articles