H5W3
当前位置:H5W3 > java > 正文

【Java】Java控制台打印当前月份

代码

package com.learning;
import java.time.DayOfWeek;
import java.time.LocalDate;
public class CalendarTest {
public static void main(String[] args){
LocalDate date = LocalDate.now();
int month = date.getMonthValue();
int today = date.getDayOfMonth();
date = date.minusDays(today - 1); // 设置本月起始日期
DayOfWeek weekday = date.getDayOfWeek();
int value = weekday.getValue(); // 1:monday ... 7 :sunday
System.out.println("Mon Tue Wed Thu Fri Sat Sun");
for(int i = 1; i < value; i++)
System.out.print("    ");
while(date.getMonthValue() == month){
System.out.printf("%3d",date.getDayOfMonth());
if(date.getDayOfMonth() == today)
System.out.print("*");
else System.out.print(" ");
date = date.plusDays(1);
if(date.getDayOfWeek().getValue() == 1)
System.out.println();
}
}
}

效果

Mon Tue Wed Thu Fri Sat Sun
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
Process finished with exit code 0

本文地址:H5W3 » 【Java】Java控制台打印当前月份

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址