Android time difference with joda time

僤鯓⒐⒋嵵緔 提交于 2019-12-04 05:57:24

问题


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();

回答1:


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;



回答2:


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).



来源:https://stackoverflow.com/questions/11177803/android-time-difference-with-joda-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!