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

    简介

    示例代码

    Result

    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 
    

    和 windows 里面的完全一致

    简要分析

    package test1;
    import java.time.*;
    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(); // 得到当前月份第一天是这一周的第几天
    		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); // date++
    			if(date.getDayOfWeek().getValue() == 1) System.out.println();// 得到第一天换行
    		}
    		if (date.getDayOfWeek().getValue() != 1) System.out.println(); // 得到月末 换行
    	}
    }
    
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    leetcode:Swap Nodes in Pairs
    leetcode:Coin Change
    leetcode:Odd Even Linked List
    算法的时间复杂度和空间复杂度
    linux学习之centos(三):网卡配置
    VMware虚拟机中的常用文件介绍
    leetcode:Partition List
    REST简析
    数据结构与算法之——五大查找
    Lepus经历收获杂谈(二)——QT
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13364351.html
Copyright © 2011-2022 走看看