Android time difference with joda time

2020-05-08 02:19发布

I have this simple code, using Joda-time.
Works fine, but I have a problem.
For example, it returns 814 minutes, what by the code, it's ok.
But I want the result to be less than 60 minutes, not the 814 minutes.
So, how can I convert that 814 minutes to get the result that I want ?

The same happens in Hours.

Thank you.

The code:

int days = Days.daysBetween(new DateTime(today), new DateTime(StartTime1)).getDays();

int hours =Hours.hoursBetween(new DateTime(today), new DateTime(StartTime1)).getHours();

int minutes = Minutes.minutesBetween(new DateTime(today),new DateTime(StartTime1)).getMinutes();

2条回答
Ridiculous、
2楼-- · 2020-05-08 03:00

I suspect you're asking the wrong question of Joda-Time. Try looking at the Period class:

Period p = new Period(new DateTime(today), new DateTime(StartTime1), PeriodType.dayTime());
int days = p.getDays();
int hours = p.getHours();

ie Period breaks the time down into days and hours for you, correctly handling daylight savings time (which your original method and manual % calculations do not).

查看更多
Anthone
3楼-- · 2020-05-08 03:07

You have the total number of minutes, so you need to get the remainder of the minutes divided by 60 minutes (assuming you are keeping track of the hours in your hours variable):

minutes = minutes % 60;
查看更多
登录 后发表回答