954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Get days in a specific month

maxDays = calendar.getActualMaximum(calendar.DAY_OF_MONTH);
System.out.println(maxDays);

When looking through the javadocs it seems like this would work but it apparently doesn't. Any help on figuring why it doesn't or another way would be appreciated

white feather
Light Poster
49 posts since Apr 2010
Reputation Points: 18
Solved Threads: 2
 
Calendar myCal = Calendar.getInstance();
System.out.println(myCal.getActualMaximum(Calendar.DATE));


That works, I just tested it on my machine. And according to the Javadoc, DATE is a synonym for DAY_OF_MONTH so day of month should work as well. Although it won't mess anything up, you shouldn't use your instance variable (calendar) to refer to a static field (DAY_OF_MONTH). And of course, that method will return the maximum value for the current month (April) so if you are concerned with a different month you'll have to set the Calendar explicitly first.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

Well that code (as is) works. But when i set the month, it prints out 31. So im guessing my attempt at setting didnt work

Calendar myCal = Calendar.getInstance();
		myCal.set(2010,2,1);
		System.out.println(myCal.getActualMaximum(Calendar.DATE));

and the strange thing is when i print out the month using

System.out.println(myCal.get(Calendar.MONTH));

it prints out 2.
Thanks for helping

white feather
Light Poster
49 posts since Apr 2010
Reputation Points: 18
Solved Threads: 2
 

Ah, fixed it. Apparently according to my code, jan had28 days and feb had 31 lol. Thanks for the help

white feather
Light Poster
49 posts since Apr 2010
Reputation Points: 18
Solved Threads: 2
 

No problem - mark solved threads as solved

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: