所以,我试图计算两个日期之间的差异,并将这些差异分为几天,几小时和几分钟。我一直在运行这个示例代码就像一个java控制台应用程序,它工作正常,但是,当我尝试在Android中做同样的事情时,我得到了垃圾数据。 Joda Time对于Android来说太复杂了吗?我应该尝试使用Date4j吗?
import java.util.; import org.joda.;
import org.joda.time.DateTime;
public class time {
//(5000);
final static long MILLIS_IN_DAY = 86400000;
final static long MILLIS_IN_HOUR = 3600000;
final static long MILLIS_IN_MINUTE = 60000;
public static void main(String[] args){
long day, hour, minute;
//MAKE SURE TO CHECK IF USER SET(LATER)
// DateTime(2013,9,12,0,0)
DateTime d1 = new DateTime(2013,9,17,0,0);
DateTime d2 = new DateTime(2013,9,12,21,30);
//long date = d2.getMillis();
DateTime d3 = new DateTime();
long diffInMillis = d3.getMillis() - d1.getMillis();
minute = (diffInMillis/MILLIS_IN_MINUTE)%60;
hour = (diffInMillis/MILLIS_IN_HOUR)%24;
day = (diffInMillis/MILLIS_IN_DAY);
System.out.println(Long.toString(d1.getMillis()));
System.out.println(Long.toString(d2.getMillis()));
System.out.println(Long.toString(d3.getMillis()));
System.out.println(Long.toString(diffInMillis));
System.out.println("Minutes:");
System.out.println(Long.toString(minute));
System.out.println("Hours:");
System.out.println(Long.toString(hour));
System.out.println("Days:");
System.out.println(Long.toString(day));
}
}
答案 0 :(得分:3)
请勿手动计算此值。用Joda-Time方法计算。没有理由使用Joda
并且不使用Joda
的方法
例如
使用Days,Hours,Mintes
System.out.println("Minutes:");
System.out.println(Minutes.minutesBetween(d2, d1).getMinutes() % 60);
System.out.println("Hours:");
System.out.println(Hours.hoursBetween(d2, d1).getHours() % 24);
System.out.println("Days:");
System.out.println(Days.daysBetween(d2, d1).getDays());
Period period = new Period(d2, d1);
System.out.println("Minutes:");
System.out.println(period.getMinutes());
System.out.println("Hours:");
System.out.println(period.getHours());
System.out.println("Days:");
System.out.println(period.getDays());