java8 Instant 万年历

package com.wq.main;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.Scanner;

/**
 * @version 创建时间:2018年8月1日 上午10:25:02 java8 Instant 万年历
 */
public class InstantDate {
    private static Scanner sc = new Scanner(System.in);
    private static String year = "";
    private static String month = "";
    private static String day = "";
    static final Integer NUMBER_ONE = 1;

    public static void main(String[] args) throws ParseException {
        Integer years = year();
        Integer months = month();
        Integer days = day();
        Calendar(years, months, days);
    }

    public static void Calendar(Integer year, Integer month, Integer day) {
        // 根据年月算出每个月一号是星期几
        Integer week = LocalDate.of(year, month, NUMBER_ONE).getDayOfWeek().getValue();
        // 根据年月算出月的天数,能区分润年平年
        Integer days = YearMonth.of(year, month).lengthOfMonth();
        System.out.println("\t" + year + "年\t" + month + "月" + day + "日");
        System.out.println("日\t一\t二\t三\t四\t五\t六");
        for (int j = 0; j < (week == 7 ? 0 : week); j++) {
            System.out.print("\t");
        }
        for (int j = 1; j <= days; j++) {
            if (j == day) {
                System.out.print(j + "*" + "\t");
            } else {
                System.out.print(j + "\t");
            }
            if ((week + j) % 7 == 0) {
                System.out.print("\n");
            }
        }
    }

    // 判断年份输入是否合法
    private static Integer year() {
        System.out.println("请输入年:");
        while (true) {
            year = sc.nextLine();
            if (year.length() == 0 || year == null) {
                System.out.println("请输入年:1900-9999_null");
            } else if (!year.matches("[0-9]+")) {
                System.out.println("请输入年:1900-9999_不是数字");
            } else if (Integer.parseInt(year) < 1900 || Integer.parseInt(year) > 9999) {
                System.out.println("请输入年:1900-9999_不在范围");
            } else {
                return Integer.parseInt(year);
            }
        }
    }

    // 判断月份输入是否合法
    private static Integer month() {
        System.out.println("请输入月:");
        while (true) {
            month = sc.nextLine();
            if (month.length() == 0 || month == null) {
                System.out.println("请输入月:1-12_null");
            } else if (!month.matches("[0-9]+")) {
                System.out.println("请输入月:1-12_不是数字");
            } else if (Integer.parseInt(month) < 1 || Integer.parseInt(month) > 12) {
                System.out.println("请输入月:1-12_不在范围");
            } else {
                return Integer.parseInt(month);
            }
        }
    }

    // 判断月天数输入是否合法
    private static Integer day() {
        System.out.println("请输入日:");
        // 根据年月算出月的天数,能区分润年平年
        Integer days = YearMonth.of(Integer.parseInt(year), Integer.parseInt(month)).lengthOfMonth();
        while (true) {
            day = sc.nextLine();
            if (day.length() == 0 || day == null) {
                System.out.println("请输入日:1-" + days + "_null");
            } else if (!day.matches("[0-9]+")) {
                System.out.println("请输入日:1-+" + days + "+_不是数字");
            } else if (Integer.parseInt(day) < 1 || Integer.parseInt(day) > days) {
                System.out.println("请输入日:1-" + days + "_不在范围");
            } else {
                return Integer.parseInt(day);
            }
        }
    }
}

运行效果图:
效果图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值