1. 程式人生 > >java Calendar 判斷閏年、平年

java Calendar 判斷閏年、平年

import java.util.Calendar;
import java.util.Scanner;

public class App {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("輸入年份");
		int year = sc.nextInt();
		
		Calendar cal = Calendar.getInstance();
		cal.set(year, 2, 1);

		cal.add(Calendar.DAY_OF_MONTH, -1);
		if (cal.get(Calendar.DAY_OF_MONTH) == 29) 
			System.out.println(year + "是閏年");
		else
			System.out.println(year + "是平年");
	}
}