a- package demo;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.Scanner;
- public class GetDaysOfMonth {
- /**
- * 计算某个月的天数
- */
- public static void main(String[] args) {
- System.out.println("输入年:");
- int year = Integer.parseInt(new Scanner(System.in).nextLine());
- System.out.println("输入月:");
- int month = Integer.parseInt(new Scanner(System.in).nextLine());
- Calendar cal = Calendar.getInstance();
- // cal.set(Calendar.YEAR, year);
- // cal.set(Calendar.MONTH,month+1);
- cal.set(year, month, 1); // 实际月份的 Calendar.MONTH + 1;
- Date d = cal.getTime();
- // System.out.println(d);
- cal.add(Calendar.DAY_OF_MONTH, -1);
- System.out.println(year + "年" + month + "月的天数:"
- + cal.get(Calendar.DAY_OF_MONTH));
- }
- }
复制代码
|
|