Java获取当前月底时间 java calendar获取月份_System

 

 

package com.cyl.test3;

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

public class RiLi {
    public static void main(String[] args){
        int year= 2020;
        int month = 2;

        Calendar calendar = Calendar.getInstance();
        //由于Java中月份从0月开始计数,所以真实月份12月(month=12) 需要减1 才是Java中的12月
        calendar.set(year,month-1,1);//每月首日星期几
        int weekday = calendar.get(Calendar.DAY_OF_WEEK)-1;
        System.out.println("当月首日星期" + weekday);

        int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        System.out.println("当月最大天数" + maxDay);
        System.out.println("\t\t"+year+"年"+month+"月");
        System.out.println("一\t二\t三\t四\t五\t六\t日\t");
        int temp = 0;
        //补空格
        for(int i = 0; i < weekday-1;i++)
        {
            System.out.print("\t");
            temp++;
        }

        for(int i = 1; i <= maxDay;i++)
        {
            System.out.print(i+"\t");
            if((temp+i)%7==0)
            {
                System.out.println();
            }
        }

    }
}