zoukankan      html  css  js  c++  java
  • java打印日历

    简单的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 }

    输出结果:

  • 相关阅读:
    POJ2406【KMP-next数组】
    POJ2752【KMP-next数组含义】
    POJ3696【欧拉函数+欧拉定理】
    POJ3692【二分匹配】
    POJ3697【BFS】
    CodeForces599D【数学】
    CodeForces599C【贪心】
    HDU1829【种类并查集】
    HDU3038【种类并查集】
    POJ1182【种类并查集】
  • 原文地址:https://www.cnblogs.com/ikuman/p/2803749.html
Copyright © 2011-2022 走看看