java - Number Of Days in a month of particular year Not Working -
note: donot mark duplicate, know duplicate question didn't it.
i want calculate number of days in month of particular year. read this , not working fine. tried following
public class numofdays { public static void main(string[] args) { scanner input = new scanner(system.in); system.out.print("enter month: "); int month = input.nextint(); system.out.print("enter year: "); int year = input.nextint(); calendar mycal = new gregoriancalendar(year, month, 1); system.out.println("number of days are: " + mycal.getactualmaximum(calendar.day_of_month)); } }
my console is
enter month: 2 enter year: 2000 number of days are: 31 /// wrong
and
enter month: 10 enter year: 1999 number of days are: 30 // correct
i know there 1 other method i.e. calculate manually, want above. please let me know if doing wrong.
p.s: using java-8.
wasn't there 31 days in march 2000? month 0 based, doc (http://docs.oracle.com/javase/8/docs/api/java/util/gregoriancalendar.html#gregoriancalendar-int-int-int-)
edit: i'm stupid, thought had number of days in month figured out. need use getactualmaximum, not get. (http://docs.oracle.com/javase/8/docs/api/java/util/gregoriancalendar.html#getactualmaximum-int-)
Comments
Post a Comment