如果想用户自定义输入日期查询,可以通过Calendar的set方法和Scanner方法设置
Calendar类简单使用:https://blog.csdn.net/weixin_43670802/article/details/89953759
package test1.three;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MyCalendar {
public static void printCalendar(){
//本月时长,打印变量,当前日期
int days=0,n=1,div=0;
SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月");
Calendar now=Calendar.getInstance();
//-------------------------------------------------
//得到当前日期
div=now.get(Calendar.DAY_OF_MONTH);
//-------------------------------------------------
//得到本月天数
now.add(Calendar.MONTH,1);
now.set(Calendar.DAY_OF_MONTH,1);
now.add(Calendar.DAY_OF_MONTH,-1);
days=now.get(Calendar.DAY_OF_MONTH);
//-------------------------------------------------
//当前月份日历显示
System.out.println(" 傻瓜式日历:"+format.format(now.getTime()));
System.out.println(" --------------------------------------------------");
//-------------------------------------------------
//设置为月初
now.set(Calendar.DAY_OF_MONTH,1);
//-------------------------------------------------
//表头
String[] week={"日","一","二","三","四","五","六"};
//-------------------------------------------------
//打印表头
for(int i=0;i<week.length;i++){
System.out.print(" "+week[i]);
}
//换行
System.out.print("
");
//-------------------------------------------------
//打印第一行
for(int i=1;i<now.get(Calendar.DAY_OF_WEEK);i++){
System.out.print(" ");
}
for(int j=0;j<=7-now.get(Calendar.DAY_OF_WEEK);j++){
//判断是否超出本月期限
if(n<=days){
//判断是否是当前日
if(n==div)
System.out.print("["+(n++)+"]");
else
System.out.print(n++);
}
System.out.print(" ");
}
System.out.print("
");
//-------------------------------------------------
//打印其余部分
for(int i=0;i<days/7+1;i++){
//7列
for(int j=0;j<7;j++){
//判断是否超出本月期限
if(n<=days){
//判断是否是当前日
if(n==div)
System.out.print("["+(n++)+"]");
else
System.out.print(n++);
}
//间距
System.out.print(" ");
}
//换行
System.out.print("
");
}
//-------------------------------------------------
}
}
另外还有好多更简便的实现方法,参考
https://blog.csdn.net/bj15114817915/article/details/62431237/
https://blog.csdn.net/madridcrls7/article/details/80270764