Time difference - strange result

I have a very simple code that calculates the difference between two points:

import java.text.SimpleDateFormat; import java.util.Date; import java.util.GregorianCalendar; public class JavaApplication8 { private static final SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss.SSS"); public static void main(String[] args) throws InterruptedException { Date start = GregorianCalendar.getInstance().getTime(); Thread.sleep(100); Date end = GregorianCalendar.getInstance().getTime(); long diff = end.getTime() - start.getTime(); System.out.println(timeFormat.format(diff)); } } 

but it prints 01:00:00.100 instead of 00:00:00.100 , why?

+4
source share
4 answers

Another way to solve this problem. In fact, the time difference you have is not equal to the milliseconds of the current time. This is just a time difference, so make a simple split that can have hours: mins: secs. And it's pretty fast.

 Date start = GregorianCalendar.getInstance().getTime(); Thread.sleep(100); Date end = GregorianCalendar.getInstance().getTime(); long longVal = end.getTime() - start.getTime(); long hours = longVal / 3600000; long mins = (longVal % 3600) / 60000; long secs = longVal % 60000; System.out.println(hours + " " + mins + " " + secs); 
+5
source

This is a time zone issue. DateFormat.format() will by default format the date in your default time zone, which looks like UTC+1 .

You must set the time zone for timeFormat to UTC :

 timeFormat.setTimeZone(TimeZone.getTimeZone("UTC")); System.out.println(timeFormat.format(diff)); 

In addition, you should use HH instead of HH in your DateFormat. HH - for 12 hour hours.

 new SimpleDateFormat("HH:mm:ss.SSS"); 

Update:

But there are other serious problems. You are currently trying to format a duration that you should not do. The Java Date APIs have no concept of period or duration.

The value you get from formatting is nothing more than the number of milliseconds (equivalent to the difference) from an era. Instead, it returns a Date object. Although the result may seem correct, but technically the difference between the two dates means a period or duration, which does not coincide with the date (which indicates a specific point in time).

You should consider switching to Joda Time for this task, which has classes that represent these concepts, such as Period , Instant and Duration .

+4
source

You have mixed up two concepts:

  • You measure the time interval (the difference between two points in time)
  • You print the date (one single point in time)

Both are incompatible, you will always get such strange effects. In your case, as indicated in other comments, the time zone is mixed. The concept of time zones exists only for dates (point in time), but does not make sense for intervals.

You can use the Jodatime library or the JSR 310 API: Date and Time (I think I think it is Java 8).

With Jodatime, you can explicitly plot the interval:

 DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0); DateTime end = new DateTime(2005, 1, 1, 0, 0, 0, 0); Period period = new Period(start, end); 

and then format it using the PeriodFormatter parameter

 PeriodFormatter daysHoursMinutes = new PeriodFormatterBuilder() .appendDays() .appendSuffix(" day", " days") .appendSeparator(" and ") .appendMinutes() .appendSuffix(" minute", " minutes") .appendSeparator(" and ") .appendSeconds() .appendSuffix(" second", " seconds") .toFormatter(); System.out.println(daysHoursMinutes.print(period)); 

By dividing the concepts of ONE moment in time and time between two points in time, you can make sure that there are no other surprises (for example, seconds of a jump).

+4
source

Because your time zone is GMT + 1.

If you read the documentation , you will find that getTime() does:

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

Thus, 100 ms means January 1, 1970, 00:00:00.100 GMT , which is equal to January 1, 1970, 01:00:00.100 GMT+1 .

You can simply set the time zone to which you want to convert your time:

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

And you want a 24-style format: HH:mm:ss.SSS , otherwise it will show 12 instead of 00 hours.

EDIT : as jarnbjo said: you are trying to apply a date format to an interval of time that obviously will not work the way you expect. There are no such time slots in the Java API. You will need to write your own.

0
source

Source: https://habr.com/ru/post/1495755/


All Articles