0

I am trying to find difference between 2 times in minutes with following code.not providing complete code:

   String dateStart = "03/25/2014 18:03:00";

String dateStop = "03/25/2014 19:45:00";


SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

Date d1 = null;

Date d2 = null;



d1 = format.parse(dateStart);

d2 = format.parse(dateStop);    


long diff = endDate.getTime()-startDate.getTime();

long minutes = diff/(60*1000) % 60;

not sure why it is returnung 14 minutes. instead 102 minutes.

Regards, chaitu

4 Answers4

1

Joda-Time

In Joda-Time, use the Minutes class. Basically one line of code, calling minutesBetween.

Example Code

Here is some example code in Joda-Time 2.3.

Parsing those strings

DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTimeFormatter formatter = DateTimeFormat.forPattern( "MM/dd/yyyy HH:mm:ss" ).withZone( timeZone );
DateTime start = formatter.parseDateTime( "03/25/2014 18:03:00" );
DateTime stop = formatter.parseDateTime( "03/25/2014 19:45:00" );

Calculating minutes between

int minutes = Minutes.minutesBetween( start, stop ).getMinutes();

ISO Duration (Period)

Or you may want to generate a string in the ISO 8601 format of Durations: PnYnMnDTnHnMnS. To do so in Joda-Time, use the Period class (yes, date-time terminology is not standardized, used differently by different folks).

Period period = new Period( start, stop );
String output = period.toString();

Results

Dump to console…

System.out.println( "start: " + start );
System.out.println( "stop: " + stop );
System.out.println( "minutes: " + minutes );
System.out.println( "period: " + period );

When run…

start: 2014-03-25T18:03:00.000+01:00
stop: 2014-03-25T19:45:00.000+01:00
minutes: 102
period: PT1H42M

That output of PT1H42M means "one hour and forty-two minutes".

Time Zone

Your question and code ignored the crucial issue of time zone in parsing those strings. You should almost always specify a time zone.

java.time

The new java.time package in Java 8 may have similar features as what you’ve seen here with Joda-Time.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

To get the difference in minutes, you have can use:

long minutes = diff / (60 * 1000);

When you do:

long minutes = diff / (60 * 1000) % 60;

you are getting the difference of minutes between the dates. In other words, you are only considering minutes; not hours, nor days...

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0
long diff = endDate.getTime()-startDate.getTime();

makes diff the difference between the two times in milliseconds (6120000). To get seconds, divide by 1000 (6120). To get minutes, divide the result of that by 60 (102).

To get utter nonsense, further take mod 60 (gives 42).

(Note that this is a mathematical error, not really a question about Java.)

Wooble
  • 87,717
  • 12
  • 108
  • 131
  • Good explaintation thank you. my bad, i didn't notice. It gave me correct value till the difference is 59 minutes. after that it failed. –  Mar 25 '14 at 20:58
0

The modulus operation in your code isn't needed. Remember that the modulus operator gives you the remainder of an operation. If you take out the "% 60;" part of your code, it should work.

Fixed calculation:

long minutes = diff/(60*1000);

The situation where you'd want to use the modulus operator is if you wanted to express the difference also in units that are greater than a minute, such as hours or days. In this case you'd use the modulus operator to make sure you'd only extract the number of minutes that are less than 60.

Andy Niles
  • 36
  • 3