4.2math、Arrays、日期类

    科技2025-12-24  8

    Java.lang.Math

    找到休息日 package com; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Scanner; public class FindRestDay { public static void main(String[] args) throws ParseException { Scanner input = new Scanner(System.in); System.out.println("请输入年份:"); int year = input.nextInt(); System.out.println("请输入月份:"); int month = input.nextInt(); title(); start(year,month - 1); } static int[] restDay(Calendar cl, int i, int a, int day, int weekend) throws ParseException { long nonRestDay = 4*24*60*60*1000; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date day1 = sdf.parse("2020-02-02 00:00:00"); StringBuilder sb = new StringBuilder(); if ((cl.getTime().getTime() - day1.getTime()) % nonRestDay == 0 && (cl.getTime().getTime() - day1.getTime()) >= 0) { sb.append("["); sb.append(i); sb.append("]"); day++; if (a == 1 || a == 7) { weekend++; } } else { sb.append(" "); sb.append(i); } if (a == 7) { System.out.println(sb.toString()); } else { sb.append(" \t"); System.out.print(sb.toString()); } return new int[] {day,weekend}; } /** * 打印日历 * @param year * @param month */ static void start(int year, int month) throws ParseException { Calendar cl = Calendar.getInstance(); cl.clear(); cl.set(Calendar.YEAR, year); cl.set(Calendar.MONTH, month); int day = 0; int weekend = 0; for (int i = 1; i <= cl.getActualMaximum(Calendar.DAY_OF_MONTH); i++) { cl.set(Calendar.DAY_OF_MONTH, i); int a = cl.get(Calendar.DAY_OF_WEEK); if (i == 1) { for (int j = 1; j < a; j++) { System.out.print("\t\t"); } } int[] d = restDay(cl,i,a,day,weekend); day = d[0]; weekend = d[1]; } System.out.println(); System.out.println("本月休息天数有:" + day + "天"); System.out.println("本月轮到周末休息天数是:" + weekend + "天"); } static void title() { System.out.print("星期日\t"); System.out.print("星期一\t"); System.out.print("星期二\t"); System.out.print("星期三\t"); System.out.print("星期四\t"); System.out.print("星期五\t"); System.out.println("星期六"); } }
    Processed: 0.014, SQL: 9