简单的demo

 1 package com.test.transport.service;
 2 
 3 import java.text.DateFormatSymbols;
 4 import java.util.Calendar;
 5 import java.util.Locale;
 6 
 7 public class DisplayCalendar {
 8     public static void main(String args[]) {
 9         Locale.setDefault(Locale.US);
10         Calendar calendar = Calendar.getInstance();
11         calendar.setLenient(true);
12         int today = calendar.get(Calendar.DAY_OF_MONTH);//当天
13         int month = calendar.get(Calendar.MONTH);//当月
14         int firstDayOfWeek = calendar.getFirstDayOfWeek();//每周从周几开始
15         calendar.set(Calendar.DAY_OF_MONTH, 1);
16         int firstday = calendar.get(Calendar.DAY_OF_WEEK);
17         String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
18         for(int i=1;i<8;i++){
19             System.out.printf("%4s", weekdayNames[i]);
20         }
21         System.out.println();
22         do {
23             int day = calendar.get(Calendar.DAY_OF_MONTH);
24             if(day == 1){
25                 System.out.print(" ");
26                 for(int i =0;i<firstday-1;i++){
27                     System.out.print("    ");
28                 }
29             }
30             System.out.printf("%3d", day);
31             if (day == today) {
32                 System.out.print("*");
33             } else {
34                 System.out.print(" ");
35             }
36             calendar.add(Calendar.DAY_OF_MONTH, 1);
37             if (calendar.get(Calendar.DAY_OF_WEEK) == firstDayOfWeek) {
38                 System.out.println();
39                 System.out.print(" ");
40             }
41         } while (calendar.get(Calendar.MONTH) == month);
42     }
43 }

输出结果:

java打印日历

 

 

 

 

相关文章:

  • 2022-12-23
  • 2021-11-29
  • 2022-02-08
  • 2022-02-17
  • 2022-02-02
  • 2021-12-02
  • 2022-01-11
  • 2022-12-23
猜你喜欢
  • 2021-12-28
  • 2021-12-29
  • 2021-12-17
  • 2022-01-16
  • 2021-11-09
  • 2022-01-28
  • 2021-07-03
相关资源
相似解决方案