Java - Time difference in minutes

后端 未结 4 1147
不思量自难忘°
不思量自难忘° 2021-01-21 11:12

I have this problem with calculating time difference in minutes. Its working fine with exampples like calculating the difference between 2045 and 2300.

But when I want t

4条回答
  •  终归单人心
    2021-01-21 11:46

    This is a solved problem. If you look at the Joda Time library you'll find all the time and date manipulation functions you could possibly want:

    In your case something along the lines of:

    DateTime first = new DateTime(larger-time);
    DateTime second = new DateTime(smaller-time);
    DateTime difference = first.minusMillis(second.getMillis())
    

    Joda will cope with all the odd edge conditions like rolling over between days/months/years, lengths of months, leap years, daylight savings, timezones...

提交回复
热议问题