2

I have 2 times in calendar.

    Calendar orarioP1 = new GregorianCalendar(2014, 3, 28, 8, 30, 00);
    Calendar orarioA1 = new GregorianCalendar(2014, 3, 28, 12, 35, 06);  

I implemented this function for get the time difference but it return me +1hour difference.
I need to return the correct difference 4:05:06 but now it return me 5:05:06. Can anyone help me?

public String getTimeDiff() {

    SimpleDateFormat sdf = new SimpleDateFormat("h:mm:ss");
    long milliSec1 = orarioP1.getTimeInMillis();
    long milliSec2 = orarioA1.getTimeInMillis();

    long timeDifInMilliSec;
    if(milliSec1 >= milliSec2) {
        timeDifInMilliSec = milliSec1 - milliSec2;
    } else {
        timeDifInMilliSec = milliSec2 - milliSec1;
    }

    long timeDifSeconds = timeDifInMilliSec / 1000;
    long timeDifMinutes = timeDifInMilliSec / (60 * 1000);
    long timeDifHours = timeDifInMilliSec / (60 * 60 * 1000);
    long timeDifDays = timeDifInMilliSec / (24 * 60 * 60 * 1000);

    Date date = new Date(timeDifSeconds);
    String difference = sdf.format(date);

    return difference;
}
vinS
  • 1,417
  • 5
  • 24
  • 37
Marc C
  • 73
  • 1
  • 8
  • Same issue? http://stackoverflow.com/questions/3299972/difference-in-days-between-two-dates-in-java – My-Name-Is Aug 10 '14 at 15:16
  • These things are almost always a result of Daylight Saving Time or a timezone difference. A quick look [here](http://www.timeanddate.com/time/dst/2014.html) shows us that this date is indeed around the time DST sprung into action. – Jeroen Vannevel Aug 10 '14 at 15:16
  • [This approach](http://stackoverflow.com/a/9027379/256196) may help. – Bohemian Aug 10 '14 at 15:37
  • possible duplicate of [Calculating the Difference Between Two Java Date Instances](http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) – ericbn Aug 10 '14 at 17:05
  • Tip: (A) Learn about [Joda-Time](http://www.joda.org/joda-time/) library with its Duration, Interval, and Period classes to handle a span of time. The new java.time package in Java 8 may have similar. (B) Always specify your intended time zone in your code. – Basil Bourque Aug 10 '14 at 19:43

3 Answers3

0

You craete a Date 1970-01-01 04:05:06 GMT and it gets shifted to your current timezone. Either format time difference on your own ore better use Joda Time library.

Use Period class from Joda Time. Docs here

Your code:

   public String getTimeDiff() {
      DateTime start = new DateTime(2014, 3, 28, 8, 30, 00);
      DateTime end = new DateTime(2014, 3, 28, 12, 35, 06);
      Period period = new Period(start, end);
      return PeriodFormat.getDefault().print(period);
  }

Returns: 4 hours, 5 minutes and 6 seconds

Piotr Gwiazda
  • 12,080
  • 13
  • 60
  • 91
0

You are misunderstanding it. new Date(long timemillisec) will return a Date instance of since January 1, 1970, 00:00:00 GMT + your timemillisec.

It is your time zone which is making January 1, 1970, 00:00:00 to somewhat nearly January 1, 1970, 05:~:~

just use :

String difference = sdf.format(new Date(0));

you will get the same result i.e. 1 hour difference in your case, it is due to time difference from GMT.
And have a look at your code, you are converting milliseconds to seconds, you don't need that.
just use millisecs : Date date = new Date(timeDifInMilliSec);

have a look at this :

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MMM/dd h:mm:ss");
 System.out.println(sdf.format(new Date(0)));
 System.out.println(sdf.format(new Date(1000)));

Output

1970/Jan/01 5:30:00
1970/Jan/01 5:30:01

It is for my location.

afzalex
  • 8,598
  • 2
  • 34
  • 61
  • i tried to change String difference = sdf.format(date); to String difference = sdf.format(new Date(0)); but it give me other result different from the real result! – Marc C Aug 10 '14 at 15:58
  • First you have to correct your error. In your code you must take `timeDifInMilliSec` instead of `timeDifSec`. your code is returning you `sdf.format(new Date(0).getTime() + timeDifInMilliSec)` @MarcC – afzalex Aug 10 '14 at 16:03
  • i changed timeDifSec to timeDifInMillisec and changed sdf.format(date) to sdf.format(new Date(0)) but it return me 1:00:00, not the correct value 4:05:06 @afzalex – Marc C Aug 10 '14 at 16:50
  • You must be kidding. `1:00:00` can only be returned if your GMT time zone difference is 0.@MarcC . I am editting my answer and adding outputs from my place. – afzalex Aug 10 '14 at 16:58
0

Try something like this

    Calendar orarioP1 = new GregorianCalendar(2014, 3, 28, 8, 30, 00);
    orarioP1.setTimeZone(TimeZone.getTimeZone("GMT"));
    Calendar orarioA1 = new GregorianCalendar(2014, 3, 28, 12, 35, 06);
    orarioA1.setTimeZone(TimeZone.getTimeZone("GMT"));
    System.out.println("Difference In Hours : "
       +(orarioA1.get(Calendar.HOUR_OF_DAY)-orarioP1.get(Calendar.HOUR_OF_DAY)));
    System.out.println("Difference In Minutes : "
        +(orarioA1.get(Calendar.MINUTE)-orarioP1.get(Calendar.MINUTE)));
    System.out.println("Difference In Seconds : "
        +(orarioA1.get(Calendar.SECOND)-orarioP1.get(Calendar.SECOND)));
    System.out.println("Difference In MiliSeconds : "
       +(orarioA1.get(Calendar.MILLISECOND)-orarioP1.get(Calendar.MILLISECOND)));
SparkOn
  • 8,806
  • 4
  • 29
  • 34