如何计算经过的总天数



可能的重复项:
在 Java 中计算日期/时间差

我为用户提供了使用日期选择器选择日期的选项。是否有任何内置方法,我可以使用它来计算用户选择日期和今天日期的持续时间(以天为单位)。

我不喜欢回答这个问题,因为有数百万个这样的问题(在发布问题之前使用搜索选项)。使用乔达时间。有一个Period类,对您很有用。

以毫秒为单位获取两次之间的差值。你可以通过Java的日历类获得天数。

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

最新更新