* 如何获取任意年份的2月份有多少天
*
* 1.构造一个日历:指定年,3,1
* 2.将"day_Of_Month" 减 1
* 3.获取日期;
*/
public class Demo {
public static void main(String[] args) {
getFeb(1995);
}
// 获取任意年份的2月分天数
private static void getFeb(int year) {
// 构造一个日历:指定年,3月,1日
GregorianCalendar gc = new GregorianCalendar(year, 3 - 1, 1);
// 3月1日-1,就是2月份的最后一天,也就是2月的天数
gc.add(Calendar.DAY_OF_MONTH, -1);
// 获取天数
int month = gc.get(Calendar.DAY_OF_MONTH);
System.out.println(year + "年,2月有:" + month + "天");
}
} |
|