Java常用类之时间类

用户输入年月日,制作输出万年历的那种格式,并且在输入的日期旁标*。
在这里插入图片描述

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


public class CalendarIn {

    int year = 2021;
    int month = 7;
    int day = 21;

    public CalendarIn() {

    }

    public CalendarIn(int year,int month,int day) {

        this.year = year;
        this.month = month;
        this.day = day;
    }

    public void scanner(){

        Scanner sc = new Scanner(System.in);

        System.out.println("请输入年月日:");

        System.out.print("年:");
        this.year = sc.nextInt();
        System.out.print("月:");
        this.month = sc.nextInt();
        while (month < 0 || month >12){

            System.out.print("月份应该在1到12之间,请重新输入:");
            this.month = sc.nextInt();
        }
        System.out.print("日:");
        this.day = sc.nextInt();
        int days = CalDays();
        while (day < 0 || day >days){

            System.out.print(month + "月应该有" + days + "天,请重新输入:");
            this.day = sc.nextInt();
        }
    }

    public int CalWeek(){
        
        Calendar calendar = Calendar.getInstance();

        Date date = new Date(year-1900,month-1,1);

        calendar.setTime(date);

        return calendar.get(Calendar.DAY_OF_WEEK) -1;
    }

    public int CalDays(){

        int [] days = {31,28,31,30,31,30,31,31,30,31,30,31};

        if((year % 400 == 0) || (year % 4 ==0 & year % 100 != 0)){ // 闰年

            days[1] = 29;
        }

        return days[month - 1];
    }

    public void PrintCalendar(){

        int days = CalDays();
        int weekday = CalWeek();

        System.out.println("-------------------------------");
        System.out.println(year + "年" + month + "月");

        String [] weeks = {"日","一","二","三","四","五","六"};

        for(int i = 0; i < weeks.length;i++){

            System.out.print(weeks[i] + "\t");
        }
        System.out.println();

        int i = 0;

        while(i < weekday){

            System.out.print(" \t");
            i++;
        }

        for(int j = 1;j <= days;j++){

            if(j == day){
                System.out.print(j + "*\t");
            }
            else{
                System.out.print(j + "\t");
            }

            i++;

            if(i % 7 == 0){

                System.out.println();
                i = 0;
            }


        }
        System.out.println();
        System.out.println("-------------------------------");
    }

    public static void main(String[] args) {

        CalendarIn calendarIn = new CalendarIn();
        calendarIn.scanner();
        calendarIn.PrintCalendar();

    }
}

输出

请输入年月日:
年:2021
月:89
月份应该在112之间,请重新输入:21
月份应该在112之间,请重新输入:12
日:23
-------------------------------
202112月
日	一	二	三	四	五	六	
 	 	 	1	2	3	4	
5	6	7	8	9	10	11	
12	13	14	15	16	17	18	
19	20	21	22	23*	24	25	
26	27	28	29	30	31	
-------------------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值