I'm trying to find the sum of two periods of Joda Period. In one period, the current date and time is stored, and in the second interval.

First period date - current date and time

Date ddate = new Date(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Period date = new Period(fin.parsePeriod(dateFormat.format(ddate))); 

The second interval interval is the time interval of the format yyyy-MM-dd HH: mm: ss

 Period interval = new Period(); //0000-00-20 11:50:00 interval = interval.plusDays(20); interval = interval.plusHours(11); interval = interval.plusMinutes(50); 

I add interval to date .

 date = date.plus(interval); date = date.normalizedStandard(PeriodType.yearMonthDayTime()); 

For example, 2017-03-14 15:18:13 + 0000-00-20 11:50:00 = 2017-04-04 03:08:13 (sort of like)
But with the conclusion of this: 2017-03-35 03:08:13

How to deal with overflow? Or maybe there is another, more correct and concise approach to the task?

    1 answer 1

    Try using DateTime as the start date, but DateTime :

      DateTime date = new DateTime(); date = date.plus(interval); 

    Period is a period of time. Accordingly, with the addition of two gaps, all fields are added separately and do not affect each other. In particular, days do not affect the months.

    2017 years, 3 months and 14 days plus 20 days = 2017 years 3 months and 34 days. Transferring by months is impossible because the date of reference and, accordingly, the current month are unknown.

    Similarly, 30 hours is not 1 day and 6 hours, 366 days is not one year and day.

    • one
      It helped! Thanks for the reply and detailed explanation! - SomeNoob