0

I want to calculate the difference between a start time and an end time. In HH:mm format.

I receive a negative value when, for example, the start time is 22.00 and the end time is 1.00 the next day.

How do I let the program know the end time is on the next day?

My script:

public void setBeginTijd()
{
    String dateStart = "22:00";
    String dateEnd = "1:00";

    SimpleDateFormat format = new SimpleDateFormat("HH:mm");

    Date d1 = null;
    Date d2 = null;

    try
    {
        d1 = format.parse(dateStart);
        d2 = format.parse(dateEnd);

        long diff = d2.getTime() - d1.getTime();
        long diffMinutes = diff / (60 * 1000) % 60;
        long diffHours = diff / (60 * 60 * 1000) % 24;

        System.out.println(diffMinutes);
        System.out.println(diffHours);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
Eypros
  • 5,370
  • 6
  • 42
  • 75
BlueCastle
  • 217
  • 2
  • 8
  • 18
  • 1
    As per me, there is no way the program will know that 1:00 is next day, unless you specify the date. So your method must accept the DateTime object instead of just Time object – sanbhat Aug 22 '14 at 09:04
  • You must use full date instead of hour and minutes. Otherwise how can you recognize the day? – Thusitha Thilina Dayaratne Aug 22 '14 at 09:13
  • Where do you get the time values from anyway? Do you read them out of a log or a database? Or are they from somewhere in your program? If it's possible for you to use a time stamp or a _full_ `Date` object instead, it would give you much greater precision. – Sebastian_H Aug 22 '14 at 09:19
  • The time values come from a methods parameter. – BlueCastle Aug 22 '14 at 11:54

5 Answers5

6

If you can assume that, when the time is negative, the second time must be on the next day, then you can simply say

if (diff < 0)
{
    diff = (24 * 60 * 60 * 1000) + diff;
}

EDIT to elaborate this, also in response to the comments: Of course this is a very simplistic solution. It can not handle the case where the second date is two days later. It does not handle DST switches. It does not handle the time zone change on December 31st, 1927 in Shanghai. It is no replacement for a properly modelled date with all its caveats. It is a best-effort approach to derive what can (probably) be derived from the given information.

Community
  • 1
  • 1
Marco13
  • 53,703
  • 9
  • 80
  • 159
1

Try this

    SimpleDateFormat formatNextDay = new SimpleDateFormat("dd:HH:mm");
    boolean isNextDay=false;
    try {
        if (d1.after(d2)) {
            isNextDay=true; 
            d1 = formatNextDay.parse("1:" + dateStart);
            d2 = formatNextDay.parse("2:" + dateEnd);
        }
Tkachuk_Evgen
  • 1,334
  • 11
  • 17
  • Just solving the problem for the next day case isn't a good advice. It is very likely that further issues will appear when dates with more than one day in between are entered. By the way, the variable `isNextDay` is totally useless. – Thorben Aug 22 '14 at 09:30
0

You should include day, month and year in date.

This are dates in Java after ran program:

d1 = Thu Jan 01 22:00:00 CET 1970
d2 = Thu Jan 01 01:00:00 CET 1970
djm.im
  • 3,295
  • 4
  • 30
  • 45
0

As already mentioned by some people, it is important to also know day, month and year of each event to calculate periods for events that are not on the same day.

I modified your method the way I think it could help you:

public void setBeginTijd()
{
        String dateStart = "22.08.2014 22:00";
        String dateEnd = "25.08.2014 01:00";

        SimpleDateFormat fullFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");

        Date d1 = null;
        Date d2 = null;

        try
        {
            d1 = fullFormat.parse(dateStart);
            d2 = fullFormat.parse(dateEnd);

            long diff = d2.getTime() - d1.getTime();
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);

            System.out.println("Delta minutes: " + diffMinutes);
            System.out.println("Delta hours: " + diffHours);
            System.out.println("Delta days: " + diffDays);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
}
Thorben
  • 953
  • 13
  • 28
0

Here is the correct math for time difference in hours & minutes. Stripping of the decimal fraction is happening automatically when you operate on int/long values.

long diff = d2.getTime() - d1.getTime();
long diffHours = diff / 1000 / 60 / 60;
long diffMinutes = diff / 1000 / 60 - diffHours * 60;
Vlad
  • 1,723
  • 12
  • 16