Time difference - strange result

date, java, time

Solution

Other way to solve this. Actually time diff that you having is not millisecs of current time. Its is just time diff, so make a simple division of that u can have hours:mins:secs. And its quite 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);

Problem

I have very simple code which calculates difference between two times: ``` 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?

Original source