如何计算通过的总天数

时间:2012-09-18 13:07:00

标签: android

  

可能重复:
  Calculate date/time difference in java

我正在为用户提供使用日期选择器选择日期的选项。是否有任何内置方法可用于计算用户选择日期和今天日期的持续时间。

3 个答案:

答案 0 :(得分:1)

我不喜欢回答这个问题,因为有百万这样的问题(在发布问题之前使用搜索选项)。使用Joda Time。有一个Period类,对你有用。

答案 1 :(得分:0)

获取两次之间的差异,以毫秒为单位。你可以通过Java的Calendar课程获得Days。

答案 2 :(得分:0)

Date today = new Date(); // the date of today
Date target = new Date(); // the date of when the user picks

long todayEpoch = today.getTime(); // or can use = System.currentTimeMillis();
long targetEpoch = target.getTime();

long daysInMs = targetEpoch - todayEpoch; //days in MS's

//the # of days
float days = (daysInMs/1000/60/60/12);