Yoda Time minutes in duration or interval

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 just find out the number of days and minutes between two random dates.

This is the result 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 guess I'm doing something wrong, I just don't see that.

+5
source share
2 answers

The problem is that you are not specifying the type of period in the period constructor, so it uses the default value of "years, months, weeks, days, hours, minutes, seconds, and millions." You see only 15 minutes because you are not asking for hours that will return -18.

If you need only days and minutes, you must indicate 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

Duration, " , " Period, (, , ..) . - .

+13

, , , , , - date currentDate:

Period period = new Period(date, currentDate);
+3

All Articles