其他分享
首页 > 其他分享> > 找到休息日

找到休息日

作者:互联网

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

public class Demo1 {
    public static void main(String[] args) {
        System.out.println("请输入年:");
        Scanner input = new Scanner(System.in);
        int year = input.nextInt();
        System.out.println("请输入月:");
        int month = input.nextInt();
        System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
        Calendar c = Calendar.getInstance();//使用默认时区和区域设置获取日历。
        Calendar init = Calendar.getInstance();
        c.set(year, month-1, 1);//设置日历字段的值 YEAR , MONTH和 DAY_OF_MONTH
        init.set(2019,1,2);
        long dateInit = init.getTimeInMillis();
        int totalDay = c.getActualMaximum(Calendar.DATE);//获取这个月日期的最大值
        System.out.print(" ");
        for (int i = 1; i < c.get(Calendar.DAY_OF_WEEK); i++)
            System.out.print("        ");
        int restDay = 0;
        int restWeekday = 0;
        for (int i = 1; i <= totalDay; i++) {
            c.set(year, month-1, i);
            long date = c.getTimeInMillis();
            if(((date-dateInit+13)/(1000*3600*24))%4 == 0){
                System.out.printf("%-8s","["+i+"]");
                restDay++;
            }else{
                System.out.printf("%-8s",i);
            }
            if (c.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY)
                System.out.print("\n ");
        }
        System.out.println("\n本月休息天数有:"+restDay+"天");
    }
}

标签:init,找到,System,int,休息日,input,Calendar,out
来源: https://blog.csdn.net/weixin_48289305/article/details/118662380