Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 持续时间或间隔内的Joda时间分钟_Java_Date - Fatal编程技术网

Java 持续时间或间隔内的Joda时间分钟

Java 持续时间或间隔内的Joda时间分钟,java,date,Java,Date,我有一个简单的代码: 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 P

我有一个简单的代码:

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

我猜我做错了什么,我只是看不出是什么。

看起来它工作得很好,要获得正值,只需交换
date
currentDate

Period period = new Period(date, currentDate);

问题是您没有在周期构造函数中指定周期类型,因此它使用默认值“年、月、周、日、小时、分钟、秒和毫秒”。你只看了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

了解
持续时间
期间之间的区别很重要,前者是“一定数量的毫秒,可以根据不同的单位获取”,后者是从一组字段类型(分钟、月、日等)到值的有效映射。一段时间内没有一个单一的时间值——它是一组值。

我认为OP关注的是“分钟”的一个视图给出15,另一个视图给出1095…@LouisWasserman:我在将Joda time移植到.NET方面有点特殊,所以我可能比大多数人对它有更好的理解:)