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

直接上代码

 import java.time.LocalDate;
import java.util.Scanner; public class Calendar{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要查询的日期(年-月-日用-分隔开)");
String input = sc.nextLine();
String[] str = input.split("-");
int year = Integer.parseInt(str[0]);
int month = Integer.parseInt(str[1]);
int day = Integer.parseInt(str[2]);
LocalDate date = LocalDate.of(year, month, day); //将输入的数值创建一个LocalDate对象
String time = year + "年" + month + "月的日历";
date = date.minusDays(day - 1); //不论输入的是几号,总是从第一天算起
int value = date.getDayOfWeek().getValue(); //周一到周日对应1-7
System.out.println(" " + time);
System.out.println();
System.out.println(" 周一 周二 周三 周四 周五 周六 周日"); //这里空格的排序根据个人来定,我这看起来这么排顺序能对上
for (int i = 1; i < value; i++)
System.out.print(" ");
while (date.getMonth().getValue() == month) { //如果这个月遍历完就停止
if (date.getDayOfMonth() < 10) { //排版需要
System.out.print(" " + date.getDayOfMonth() + " ");
}
else if (date.getDayOfMonth()>=10){
System.out.print(" " + date.getDayOfMonth());
}
if (date.getDayOfMonth() == day) { //输入日期标注*重点
System.out.print("*");
} else {
System.out.print(" ");
}
date = date.plusDays(1); //每次打印完就向后一天,无需管一个月是28天还是30,31天
if (date.getDayOfWeek().getValue() == 1) {
System.out.println();
}
}
}
}

JAVA编写简单的日历,输入日期即可查看日历的更多相关文章

  1. java编写简单的语法分析预测程序

    编译原理课程中,编了一个简单的语法分析预测程序,这个程序时根据固定的文法得到预测分析表,然后编写程序来判断表达式是否会正确推到出来. 前提是程序没有左递归符合LL(1)文法: 文法如下: E→TE' ...

  2. 利用Java编写简单的WebService实例

    使用Axis编写WebService比較简单,就我的理解,WebService的实现代码和编写Java代码事实上没有什么差别,主要是将哪些Java类公布为WebService. 以下是一个从编写測试样 ...

  3. 利用Java编写简单的WebService实例-转载

    使用Axis编写WebService比较简单,就我的理解,WebService的实现代码和编写Java代码其实没有什么区别,主要是将哪些Java类发布为WebService.下面是一个从编写测试例子到 ...

  4. java编写简单的累加程序

    编程思路:1.建立类包demo: 2.在类包中建立CommanParameter类: 3.利用for循环通过强制类型转换将在后台中输入的String类型的字符转换为整型并进进累加操作: package ...

  5. java基础22 日期类、日历类、日期格式类

    package com.dhb.code; import java.text.ParseException; import java.text.SimpleDateFormat; import jav ...

  6. Java初学者作业——编写 Java 程序,在控制台中输入日期,计算该日期是对应年份的第几天。

    返回本章节 返回作业目录 需求说明: 编写 Java 程序,在控制台中输入日期,计算该日期是对应年份的第几天. 实现思路: (1)声明变量 year.month和 date,用于存储日期中的年.月.日 ...

  7. Java之从头开始编写简单课程信息管理系统

    编写简单的课程管理系统对于新手并不友好,想要出色的完成并不容易以下是我的一些经验和方法 详情可参考以下链接: https://www.cnblogs.com/dream0-0/p/10090828.h ...

  8. 初识Java程序,编写简单代码?

    Dear All: 初识Java程序,编写简单代码? 首先小编在这里说下我们今天编写Java程序使用的是 eclipse 开发工具! 1.下载eclipse 官网地址:http://www.eclip ...

  9. 使用Java编写一个简单的Web的监控系统cpu利用率,cpu温度,总内存大小

    原文:http://www.jb51.net/article/75002.htm 这篇文章主要介绍了使用Java编写一个简单的Web的监控系统的例子,并且将重要信息转为XML通过网页前端显示,非常之实 ...

随机推荐

  1. Statement与PreparedStatement区别

    1.性能区别 Statement statement = conn.createStatement(); PreparedStatement preStatement = conn.prepareSt ...

  2. 面向对象进阶-item系列、__new__、__hash__、__eq__ (四)

    item系列 dic = {'k':'v'}# 对象 : 存储属性 和调用方法dic['k'] = 'v'# class Foo:#     def __init__(self,name,age,se ...

  3. Breadth-first Search-690. Employee Importance

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  4. MySQL(增删改查补充)

    SQL语句数据行操作补充             create table tb12(                 id int auto_increment primary key,       ...

  5. 石头剪刀布Java实现

    java实现石头剪刀布过程 首先来看石头剪刀布的所有可能情况,具体如下图 第一种思路是穷举所有可能,使用if条件语句,略显呆板和麻烦. 第二种思路,因为计算机存的是数字,所以我们可以从数字角度来找规律 ...

  6. 六:MyBatis学习总结(六)——调用存储过程

    一.提出需求 查询得到男性或女性的数量, 如果传入的是0就女性否则是男性 二.准备数据库表和存储过程 create table p_user( id int primary key auto_incr ...

  7. 【hdu6121】 Build a tree 简单数学题

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6121 我好像推得挺久的诶..... 题目大意:给你一棵有$n$个点的树,根节点为$0$,对于其余节点 ...

  8. POJ 2339

    #include <iostream> #include <algorithm> #define MAXN 205 using namespace std; char _m[M ...

  9. Linux开发端口的基本操作命令

    Linux端口操作命令 查看开放端口:firewall-cmd --list-all 开发8080端口 --permanent 代码永久开发:firewall-cmd --add-port=8080/ ...

  10. 再学Java 之 private、protected、public和default的作用域

    前言:如果提到protected的访问控制级别,您的第一反应是”只能是子类才可以访问“,那么您很可能需要往下看. 首先,大致介绍一下各个访问控制符的访问控制级别(具体的介绍很多大牛的博文都有介绍,我就 ...