0

I have to get total minutes between two hour.

17:00 and 16.06 hrs

If I do like

 17.00-16.06 =0.94 

but correct answer is 54 minutes.

so logically how can I get difference minutes from two time.

without using Calender api or Joda Time.

Code:

private  double getTimeDifference(Date startDate,Date endDate, boolean sameDay)
    {
                double startTimeF=Double.valueOf(startDate.getHours()+"."+startDate.getMinutes());
                double endTimeF=Double.valueOf(endDate.getHours()+"."+endDate.getMinutes());
                double totalTime=0;
                boolean isCalculated=false;
                for(double workTime:timeMap.keySet())
                {
                        double endTimeC=timeMap.get(workTime);

                        if(startTimeF>=workTime && startTimeF<=endTimeC)
                        {
                            if(endTimeF<endTimeC && sameDay)
                            {
                                isCalculated=true;
                                totalTime+=endTimeF-startTimeF;
                            }
                            else
                            {
                                totalTime+=endTimeC-startTimeF;
                            }
                            break;
                        }
                }

                for(double workTime:timeMap.keySet())
                {
                        double endTimeC=timeMap.get(workTime);
                        if(endTimeF>=workTime && endTimeF<=endTimeC)
                        {
                            if(!isCalculated)
                            {
                                if(workTime<startTimeF && sameDay)
                                {
                                    totalTime+=endTimeF-startTimeF;
                                }
                                else
                                {
                                    totalTime+=endTimeF-workTime;
                                }
                            }

                        }
                        else if(!sameDay)
                        {
                            totalTime+=endTimeC-workTime;
                        }
                }
                return totalTime;
    }

Time map contains key and value of json string:

{"time":[{"startTime":"8:00", "endTime":"12:30", "type":"Working"},{"startTime":"12:31", "endTime":"13:00", "type":"Break"},{"startTime":"13:01", "endTime":"17:00", "type":"Working"}]}

NFE
  • 1,147
  • 1
  • 9
  • 22
  • 4
    Show us the code you have tried please. – Mengjun Nov 12 '13 at 08:16
  • Similar to question [How to calculate elapsed time from now with Joda Time?](http://stackoverflow.com/questions/2179644/how-to-calculate-elapsed-time-from-now-with-joda-time). – Basil Bourque Nov 12 '13 at 08:17
  • 1
    Try to convert the answer to minutes http://stackoverflow.com/questions/5387371/how-to-convert-minutes-to-hours-and-minutes-hhmm-in-java?rq=1 – gjman2 Nov 12 '13 at 08:17
  • 2
    Please DO NOT use floating point variables for times. `16.06` as a floating point number means 16 units and 6 hundredths. However, as a time, it means 16 units (hours) and 6 _sixtieth_ (minutes). _If_ you insist on this representation, you should convert it, i.e. 6 minutes is 1/10 of 60 minutes, so you should store 16h06 as `16.10`. Then you can subtract to get `(17.0 - 16.1) = 0.9` hours which is `0.9 * 60 = 54` minutes. However, if you want to make it easy on yourself and prevent hours of bughunting, just use a DateTime type - that's what they are for. – CompuChip Nov 12 '13 at 08:20
  • I can't use Joda time as GWT not supported it. – NFE Nov 12 '13 at 08:22
  • `java.util.Date` then? – CompuChip Nov 12 '13 at 08:23
  • @CompuChip Thank you so much for logically clarity!! – NFE Nov 12 '13 at 08:31
  • thanks to all of you. it works. – NFE Nov 12 '13 at 10:53

4 Answers4

3
long milliDifference = date1.getTime() - date2.getTime();
long minuteDifference = TimeUnit.MINUTES.convert(milliDifference, TimeUnit.MILLISECONDS);

where date1 and date2 are of type java.util.Date.

JamesB
  • 7,774
  • 2
  • 22
  • 21
  • GWT not supported TimeUnit. Line 80: No source code is available for type java.util.concurrent.TimeUnit; did you forget to inherit a required module? – NFE Nov 12 '13 at 11:41
  • Surely you just need to add java.util.concurrent.TimeUnit to your XML descriptor for the GWT module? – JamesB Nov 12 '13 at 11:45
  • No, it is support this util only. http://www.gwtproject.org/doc/latest/RefJreEmulation.html#Package_java_util – NFE Nov 12 '13 at 11:56
  • it is not supported but this things I don't know. thanks for answer. – NFE Nov 12 '13 at 14:01
2
    String firstTime = "17:00";
    String secondTime = "16:09";
    SimpleDateFormat format = new SimpleDateFormat("HH:mm");
    try {
        long diff = format.parse(firstTime).getTime() - format.parse(secondTime).getTime();
        System.out.println("Difference is " + (diff / (60 * 1000)) + " minutes.");
    } catch (ParseException e) {
        //Parsing error
    }

Output:

Difference is 51 minutes.
  • FOR Minute, Remove %60, it will give seconds not minute, plz change with like ((diff / 1000)*60) – NFE Nov 12 '13 at 13:53
  • No, it will not. getTime() method returns milliseconds value. If you divide milliseconds to 1000, it will give seconds and if you also divide seconds to 60, it will give minutes. Because 1000 milliseconds correspond to 1 second and 60 seconds correspond to 1 minute. So 1 minute means 60*1000 = 60000 milliseconds. With your solution, ((diff / 1000)*60), you mean that 60 minutes(which means 1 hour) correspond to 1000 milliseconds. – Mehmet Sedat Güngör Nov 12 '13 at 14:44
  • And about %60, I put it just because at first glimpse, I thought you will take differences of same hours but different minutes. Now I removed it and it is still working. – Mehmet Sedat Güngör Nov 12 '13 at 14:50
2

Try:

    String from="17:00";
    String to="16:06";
    String fromTime[]=from.split(":");
    String toTime[]=to.split(":");
    int fromMin=Integer.parseInt(fromTime[0])*60+Integer.parseInt(fromTime[1]);
    int toMin=Integer.parseInt(toTime[0])*60+Integer.parseInt(toTime[1]);
    System.out.println("Difference "+(fromMin-toMin)+" minutes");
Masudul
  • 21,823
  • 5
  • 43
  • 58
1

tl;dr

Duration.between( LocalTime.parse( "17:00" ) , LocalTime.parse( "16:06" ) )

java.time

The troublesome old legacy date-time classes are now supplanted by the java.time classes.

The LocalTime class represents a time-of-day without a date and without a time zone.

The Duration class tracks a span of time not attached to the timeline. Your example of 17:00 and 16:06 means a negative duration, going into the past.

LocalTime start = LocalTime.parse( "17:00" );
LocalTime stop = LocalTime.parse( "16:06" );  // Going backwards into the past for a negative duration.
Duration duration = Duration.between( start , stop );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154