Adding one month to date is not performed using Joda-Time DateTime in Java

I tried to add full months from a given start date using java DateTimeand a method plusMonths().

When my start time starts at the beginning of the month, everything works as expected:

DateTime startOfMonth = new DateTime(2013, 1, 1, 00, 00, 00);
    System.out.println(startOfMonth.toString());
    for (int i = 0; i < 12; i++) {
        startOfMonth = startOfMonth.plusMonths(1);
        System.out.println(startOfMonth.toString());
}

The conclusion is the first day of each month, as expected, and everything is fine!

2013-01-01T00:00:00.000+01:00
2013-02-01T00:00:00.000+01:00
2013-03-01T00:00:00.000+01:00
2013-04-01T00:00:00.000+02:00
2013-05-01T00:00:00.000+02:00
2013-06-01T00:00:00.000+02:00
2013-07-01T00:00:00.000+02:00
2013-08-01T00:00:00.000+02:00
2013-09-01T00:00:00.000+02:00
2013-10-01T00:00:00.000+02:00
2013-11-01T00:00:00.000+01:00
2013-12-01T00:00:00.000+01:00
2014-01-01T00:00:00.000+01:00

But when I change my example before the end of the month, it does not return what I want!

System.out.println("");
DateTime endOfMonth = new DateTime(2012, 12, 31, 23, 59, 59);
System.out.println(endOfMonth.toString());
for (int i = 0; i < 12; i++) {
    endOfMonth = endOfMonth.plusMonths(1);
    System.out.println(endOfMonth.toString());
}

This returns:

2012-12-31T23:59:59.000+01:00
2013-01-31T23:59:59.000+01:00
2013-02-28T23:59:59.000+01:00
2013-03-28T23:59:59.000+01:00
2013-04-28T23:59:59.000+02:00
2013-05-28T23:59:59.000+02:00
2013-06-28T23:59:59.000+02:00
2013-07-28T23:59:59.000+02:00
2013-08-28T23:59:59.000+02:00
2013-09-28T23:59:59.000+02:00
2013-10-28T23:59:59.000+01:00
2013-11-28T23:59:59.000+01:00
2013-12-28T23:59:59.000+01:00

So why "2013-02-28T23:59:59.000+01:00"plus one month not "2013-03-31T23:59:59.000+01:00"? Where are these three days?

+4
source share
9 answers

, . 31 , - 28. " " 31 , , , , 31 , - . , , .

. Java Date 2 3 , : -)

- ( ):

DateTime startOfMonth = new DateTime(2013, 1, 1, 00, 00, 00);
System.out.println(startOfMonth.toString());
for (int i = 0; i < 12; i++) {
    startOfMonth = startOfMonth.plusMonths(1);
    DateTime endOfMonth = startOfMonth.minusDays(1); // magic here
    System.out.println(startOfMonth + "-" + endOfMonth);
}

, [start,end), end .

+8

28 2013 . , , 28 .

doc:

, . . , 2007-03-31 2007-04-31, 2007-04-30.

+4

31, , , 28-.

. , , .

+1

, Joda - dayOfMonth.

public static void endOfMonth() {
    DateTime startOfMonth = new DateTime(2013, 1, 1, 00, 00, 00);

    for (int i = 0; i < 12; i++) {
        int lastDay = startOfMonth.dayOfMonth().getMaximumValue();
        System.out.println(startOfMonth.withDayOfMonth(lastDay).toString());

        startOfMonth = startOfMonth.plusMonths(1);
    }
}

:

2013-01-31T00:00:00.000-06:00
2013-02-28T00:00:00.000-06:00
2013-03-31T00:00:00.000-05:00
2013-04-30T00:00:00.000-05:00
2013-05-31T00:00:00.000-05:00
2013-06-30T00:00:00.000-05:00
2013-07-31T00:00:00.000-05:00
2013-08-31T00:00:00.000-05:00
2013-09-30T00:00:00.000-05:00
2013-10-31T00:00:00.000-05:00
2013-11-30T00:00:00.000-06:00
2013-12-31T00:00:00.000-06:00
+1

Half-Open

, "" . Half-Open (: [)) , , . , "" , .

Joda-Time , .

DateTimeZone timeZone = DateTimeZone.forID( "Europe/Zurich" );
DateTime firstOfYear = new DateTime( 2013, 1, 1, 0, 0, 0, timeZone ).withTimeAtStartOfDay();
Interval month01_Interval = new Interval( firstOfYear, firstOfYear.plusMonths( 1 ).withTimeAtStartOfDay() );

:

month01_Interval : 2013-01-01T00:00:00.000+01:00/2013-02-01T00:00:00.000+01:00
+1

java Calendar object, . java- , .

, Calendar :

Calendar cal = Calendar.getInstance();
cal.setTime(new Date());

cal.add(Calendar.MONTH, 1);
0

:

public static void main(String[] args) {
    Calendar endOfMonth = Calendar.getInstance();
    endOfMonth.set(Calendar.DAY_OF_MONTH, 31);
    endOfMonth.set(Calendar.MONTH, 11);
    endOfMonth.set(Calendar.YEAR, 2012);

    endOfMonth.set(Calendar.HOUR_OF_DAY, 23);
    endOfMonth.set(Calendar.MINUTE, 59);
    endOfMonth.set(Calendar.SECOND, 59);
    endOfMonth.set(Calendar.MILLISECOND, 59);

    System.out.println(endOfMonth.getTime());
    for (int i = 0; i < 12; i++) {
        endOfMonth.add(Calendar.MONTH, 1);

        if(i >= 2){
            endOfMonth.add(Calendar.MONTH, 1);
            endOfMonth.set(Calendar.DAY_OF_MONTH, 1);
            endOfMonth.add(Calendar.DAY_OF_MONTH, -1);
        }

        System.out.println(endOfMonth.getTime());
    }
}

:

Mon Dec 31 23:59:59 GMT+04:00 2012
Thu Jan 31 23:59:59 GMT+04:00 2013
Thu Feb 28 23:59:59 GMT+04:00 2013
Sun Mar 31 23:59:59 GMT+04:00 2013
Tue Apr 30 23:59:59 GMT+04:00 2013
Fri May 31 23:59:59 GMT+04:00 2013
Sun Jun 30 23:59:59 GMT+04:00 2013
Wed Jul 31 23:59:59 GMT+04:00 2013
Sat Aug 31 23:59:59 GMT+04:00 2013
Mon Sep 30 23:59:59 GMT+04:00 2013
Thu Oct 31 23:59:59 GMT+04:00 2013
Sat Nov 30 23:59:59 GMT+04:00 2013
Tue Dec 31 23:59:59 GMT+04:00 2013
0

, , . , - . :

    System.out.println("");
    DateTime endOfMonth = new DateTime(2012, 12, 31, 23, 59, 59);
    System.out.println(endOfMonth.toString());
    for (int i = 0; i < 12; i++) {
        DateTime newEndOfMonth = endOfMonth.plusMonths(i+1);
        System.out.println(newEndOfMonth.toString());
    }
0

:

new DateTime(date).plusDays(1).plusMonths(months).minusDays(1).toDate()
0

All Articles