Java for several days in February

Demand: Get any one year, how many days in February

  • 1. keyboard input in any year
  • 2. Set the calendar object of year, month, day
    year: from keyboard input
    month: Set to March, when the month starts from 0, so the value of 2 set
    day: 1st set
  • 3. March 1 to push forward, is the last day of February
  • 4. Access to this day output
public class CalendarTest {
	public static void main(String[] args) {
		
			//输入任意年份
			Scanner sc = new Scanner(System.in);
			System.out.println("请输入任意年份:");
			int year = sc.nextInt();
			
			Calendar c = Calendar.getInstance();
			c.set(year,2,1);
			
			c.add(Calendar.DATE, -1);
			
			int date = c.get(Calendar.DATE);
			System.out.println(year+"年"+"二月有"+date+"天");
		}
}
Published 98 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43472877/article/details/104378896