zoukankan      html  css  js  c++  java
  • JAVA编写简单的日历,输入日期即可查看日历

    利用LocalDate输入年月日找出当月日历

    直接上代码

     1 import java.time.LocalDate;
     2 import java.util.Scanner;
     3 
     4 public class Calendar{
     5     public static void main(String[] args) {
     6         Scanner sc = new Scanner(System.in);
     7         System.out.println("请输入要查询的日期(年-月-日用-分隔开)");
     8         String input = sc.nextLine();
     9         String[] str = input.split("-");
    10         int year = Integer.parseInt(str[0]);
    11         int month = Integer.parseInt(str[1]);
    12         int day = Integer.parseInt(str[2]);
    13         LocalDate date = LocalDate.of(year, month, day); //将输入的数值创建一个LocalDate对象
    14         String time = year + "年" + month + "月的日历";
    15         date = date.minusDays(day - 1);       //不论输入的是几号,总是从第一天算起
    16         int value = date.getDayOfWeek().getValue();   //周一到周日对应1-7
    17         System.out.println("       " + time);
    18         System.out.println();
    19         System.out.println(" 周一  周二 周三 周四 周五  周六  周日");  //这里空格的排序根据个人来定,我这看起来这么排顺序能对上
    20         for (int i = 1; i < value; i++)
    21             System.out.print("     ");
    22         while (date.getMonth().getValue() == month) {       //如果这个月遍历完就停止
    23             if (date.getDayOfMonth() < 10) {                 //排版需要
    24                 System.out.print("  " + date.getDayOfMonth() + " ");
    25             }
    26             else if (date.getDayOfMonth()>=10){
    27                 System.out.print("  " + date.getDayOfMonth());
    28             }
    29             if (date.getDayOfMonth() == day) {               //输入日期标注*重点
    30                 System.out.print("*");
    31             } else {
    32                 System.out.print(" ");
    33             }
    34             date = date.plusDays(1);                     //每次打印完就向后一天,无需管一个月是28天还是30,31天
    35             if (date.getDayOfWeek().getValue() == 1) {
    36                 System.out.println();
    37             }
    38         }
    39     }
    40 }
  • 相关阅读:
    愚公oracle数据库同步工具
    外部服务的隔离及降级
    使用redis来实现分布式锁
    php 无限极栏目 Tree 树格式 层级显示 【列表记录按顺序缩进显示】
    腾讯地图添加多marker标注样式
    MIME 视频 , 文件下载与播放权限
    php json_encode 对浮点 精度 问题 解决
    phpexcel 导入 时间格式 和 数值格式 处理
    phpexcel写入追加已有的excel文件
    php 判断字符串是否可以转 浮点型
  • 原文地址:https://www.cnblogs.com/xiaowangtongxue/p/10677728.html
Copyright © 2011-2022 走看看