我有这个简单的代码:
DateTime date = new DateTime(dateValue);
DateTime currentDate = new DateTime(System.currentTimeMillis());
System.out.println("date: " + date.toString());
System.out.println("currentDate: " + currentDate.toString());
Period period = new Period(currentDate, date);
System.out.println("PERIOD MINUTES: " + period.getMinutes());
System.out.println("PERIOD DAYS: " + period.getDays());
Duration duration = new Duration(currentDate, date);
System.out.println("DURATION MINUTES: " + duration.getStandardMinutes());
System.out.println("DURATION DAYS: " + duration.getStandardDays());
我试图简单地找出两个随机日期之间的天数和分钟数。
这是这段代码的输出:
date: 2012-02-09T00:00:00.000+02:00
currentDate: 2012-02-09T18:15:40.739+02:00
PERIOD MINUTES: -15
PERIOD DAYS: 0
DURATION MINUTES: -1095
DURATION DAYS: 0
我猜我做错了什么,我只是看不出是什么。
问题是您没有在周期构造函数中指定周期类型 - 因此它使用默认的“年、月、周、日、小时、分钟、秒和毫秒”。您只看到 15 分钟,因为您没有要求几个小时,这将返回 -18。
如果您只需要天和分钟,您应该指定:
PeriodType type = PeriodType.forFields(new DurationFieldType[] {
DurationFieldType.days(),
DurationFieldType.minutes()
});
Period period = new Period(currentDate, date, type);
// Now you'll just have minutes and days
了解 a 之间的区别很重要Duration
即“一定的毫秒数,可以根据不同的单位来取”和Period
这实际上是从一组字段类型(分钟、月份、天等)到值的映射。一个时期内不存在单个时间值 - 它是值的集合。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)