Why the day-numbers is '28' of Feb, 2000?

When I run the follow codes, the result is '28'

Date date = new Date(2000,2, 1);

Calendar cal = Calendar.getInstance();

cal.setTime(date);

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

int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

System.out.println(value);

But, I change "Date date = new Date(2000,2, 1);" to "Date date = new Date(2004,2, 1);" , The value is '29'

Thanks your help!

Best Regards

Xue

[514 byte] By [xuechenyoyoa] at [2007-11-26 21:42:02]
# 1

Sorry! I forgot the follow code:

cal.add(Calendar.YEAR, -1900);

the follow codes is right:

Date date = new Date(2000,2, 1);

Calendar cal = Calendar.getInstance();

cal.setTime(date);

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

cal.add(Calendar.YEAR, -1900);

int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

System.out.println(value);

xuechenyoyoa at 2007-7-10 3:27:48 > top of Java-index,Core,Core APIs...
# 2
Feb. only had 28 days in 1900. if (yr % 4 == 0) && (yr % 100 != 0 || yr % 400 == 0) then leap year.
jverda at 2007-7-10 3:27:48 > top of Java-index,Core,Core APIs...
# 3
This topic was posted in the wrong forum. See http://forum.java.sun.com/ann.jspa?annID=14
PeterAhea at 2007-7-10 3:27:48 > top of Java-index,Core,Core APIs...
# 4

Thanks a lots!

I am not good at the class Date and Calender.

the follow codes is ok!

After creating "Date date = new Date(2000,2, 1);", the year must do "cal.add(Calendar.YEAR, -1900);", and the month must do "cal.add(Calendar.MONTH, -1);".

Now, the problem is solved.

Date date = new Date(2000,2, 1);

Calendar cal = Calendar.getInstance();

cal.setTime(date);

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

cal.add(Calendar.YEAR, -1900);

intmonth_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

xuechenyoyoa at 2007-7-10 3:27:49 > top of Java-index,Core,Core APIs...
# 5

> Now, the problem is solved.

I'm curious. What was the original problem exactly? Your code sets the calendar time to 1 March 3900: why? Then it subtracts 1900 the hard way, when it should have been subtracted in the Date constructor. Then it calculates the number of days in January 2000. Which was 29.

If that's not what you were trying to do, you've still got a problem.

ejpa at 2007-7-10 3:27:49 > top of Java-index,Core,Core APIs...
# 6
> Then it calculates the number of days in January> 2000. Which was 29.You mean February 2000, right?
DrClapa at 2007-7-10 3:27:49 > top of Java-index,Core,Core APIs...
# 7
there are 29 days in Feb, 2000, and 28 days in Feb 1900.because of 1900%100=0, but 1900%400!=0.
xuechenyoyoa at 2007-7-10 3:27:49 > top of Java-index,Core,Core APIs...
# 8
> You mean February 2000, right?Right. I still don't know what this Date in the year 3900 was for.
ejpa at 2007-7-10 3:27:49 > top of Java-index,Core,Core APIs...
# 9

>Right. I still don't know what this Date in the year 3900 was for.

It shuold be 28.

I post the integrated codes:

/**

* Get the day numbers of the <code>month</code> in the <code>year</code>

* Example, If you want to know the day number of Feb,2000,

* the month is 2 and year is 2000.

* @param year The year that you need.

* @param month The month that you need.

* @return The number of the <code>month</code> in the <code>year</code>

*

*/

public int getDays(String year,String month){

int y=Integer.parseInt(year);

int m=Integer.parseInt(month);

Date date = new Date(y,m, 1);

Calendar cal = Calendar.getInstance();

cal.setTime(date);

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

cal.add(Calendar.YEAR, -1900);

intmonth_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

return month_day_score;

}

xuechenyoyoa at 2007-7-10 3:27:49 > top of Java-index,Core,Core APIs...
# 10

So you still have a bug because the year argument to Date(int year, int month, int day) is years since 1900, not years since 0CE or 0AD or whatever you want to call it. This is your real problem. Once you've fixed this you can forget about subtracting 1900 from the date and you'll probably then end up in the right year and get the result you're expecting.

ejpa at 2007-7-10 3:27:49 > top of Java-index,Core,Core APIs...
# 11
Hi,Thanks for your replying!But I do not understand the follow word from you,>not years since 0CE or 0ADWould you please do me a favor?A lot thanks and Best regardsXueMessage was edited by: xuechenyoyo
xuechenyoyoa at 2007-7-10 3:27:49 > top of Java-index,Core,Core APIs...
# 12

Your Javadoc says that you just pass in the year that you need, and you are parsing this as an integer and then passing the result as the first parameter to the constructor for Date. You should be subtracting 1900 from the year value at this point according to the Javadoc for java.util.Date. And therefore you don't need to subtract 1900 later on.

ejpa at 2007-7-10 3:27:49 > top of Java-index,Core,Core APIs...