Joda在持续时间或间隔内的时间分钟 [英] Joda Time minutes in a duration or interval

查看:123
本文介绍了Joda在持续时间或间隔内的时间分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的代码:

  DateTime date = new DateTime(dateValue); 
DateTime currentDate = new DateTime(System.currentTimeMillis());

System.out.println(date:+ date.toString());
System.out.println(currentDate:+ currentDate.toString());

期间=新的期间(currentDate,date);
System.out.println(PERIOD MINUTES:+ period.getMinutes());
System.out.println(PERIOD DAYS:+ period.getDays());

持续时间=新的持续时间(currentDate,date);
System.out.println(DURATION MINUTES:+ duration.getStandardMinutes());
System.out.println(DURATION DAYS:+ duration.getStandardDays());

我试图找出两个随机日期之间的天数和分钟数。 p>

这是这段代码的输出:

  date:2012 -02-09T00:00:00.000 + 02:00 
当前日期:2012-02-09T18:15:40.739 + 02:00
PERIOD MINUTES:-15
期限:0
DURATION MINUTES:-1095
DURATION DAYS:0

我猜想我'做错了事,我看不到什么。

解决方案

问题是你没有指定期间类型期间构造函数 - 所以它使用默认的年,月,周,天,小时,分钟,秒和毫秒。你只看到15分钟,因为你不需要几个小时,这将返回-18。



如果你只想要几分钟,你应该指定:

  PeriodType type = PeriodType.forFields(new DurationFieldType [] {
DurationFieldType.days(),
DurationFieldType.minutes()
});

期间=新的期间(currentDate,date,type);
//现在你只需要几分钟的时间

了解差异很重要在持续时间之间,可以根据不同的单位获取一定的毫秒数,而期间这实际上是从一组字段类型(分钟,月,日等)到值的映射。一段时间内没有一个单一时间值 - 它是值的集合。


I have this simple code:

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

I'm trying to simply find out the number of days and minutes between two random dates.

This is the output for this piece of code:

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

I'm guessing that I'm doing something wrong, I just cannot see what.

解决方案

The problem is that you're not specifying the period type in the period constructor - so it's using the default of "years, months, weeks, days, hours, minutes, seconds and millis". You're only seeing 15 minutes because you're not asking for hours, which would return -18.

If you only want days and minutes, you should specify that:

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

It's important to understand the difference between a Duration which is "a certain number of milliseconds, which can be fetched according to different units" and a Period which is effectively a mapping from a set of field types (minutes, months, days etc) to values. There isn't one single time value in a period - it's a collection of values.

这篇关于Joda在持续时间或间隔内的时间分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆