找到休息日

找到休息日

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+"天");
    }
}

上一篇:Java获取指定月份的天数


下一篇:Android studio 时间选择器及时间对话框