Why is Java Calendar installed (int year, int month, int date), does not return the correct date?

According to the doc, calendar set ():

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html#set%28int,%20int,%20int%29

set(int year, int month, int date) Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH. 

the code:

 Calendar c1 = GregorianCalendar.getInstance(); c1.set(2000, 1, 30); //January 30th 2000 Date sDate = c1.getTime(); System.out.println(sDate); 

exit:

 Wed Mar 01 19:32:21 JST 2000 

Why is it not Jan 30 ???

+42
java calendar
Feb 08 2018-11-11T00:
source share
3 answers

1 per month - February. February 30 changed to March 1. You must set 0 within a month. It is best to use the constant defined in the Calendar:

 c1.set(2000, Calendar.JANUARY, 30); 
+87
Feb 08 '11 at 10:40
source share

Months in a Calendar object start at 0

 0 = January = Calendar.JANUARY 1 = february = Calendar.FEBRUARY 
+16
Feb 08 2018-11-11T00:
source share

The selected date is interesting as an example. Code example:

 Calendar c1 = GregorianCalendar.getInstance(); c1.set(2000, 1, 30); //January 30th 2000 Date sDate = c1.getTime(); System.out.println(sDate); 

and output Wed Mar 01 19:32:21 JST 2000 .

When I first read the example, I think the conclusion is wrong, but it's true :). Calendar.Month starts at 0, so 1 means February. And you know that the last day is 28, so the release should be March 2. But the chosen year is important, this is 2000, that is, February 29, so the result should be March 1.

+4
Dec 11 2018-12-12T00:
source share



All Articles