zoukankan      html  css  js  c++  java
  • java输出万年历

    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    
    
    public class MyDate {
    	
    	  public static void main(String[] args) {
    	        MyDate date = new MyDate(); 
    	        
    	        Calendar calendar = Calendar.getInstance() ;
    	        int month = calendar.get(Calendar.MONTH ) + 1 ;
    	        calendar.set(Calendar.MONTH , month ) ; 
    	        date.myCalendar( calendar.getTime() ) ; 
    	    } 
    	    //实现日历的方法  
    	    public void myCalendar(Date date) { 
    	        GregorianCalendar now = new GregorianCalendar(); 
    	        // 打印当前时间  
    	        // 设置当前时间  
    	        now.setTime(date); 
    	        // 从日期中取得当前的日  
    	        int toDay = now.get(Calendar.DAY_OF_MONTH); 
    	        // 从日期中取得当前的月  
    	        int month = now.get(Calendar.MONTH) ; 
    	        // 设置now的日期为1  
    	        now.set(Calendar.DAY_OF_MONTH, 1); 
    	        // 得到now是一周的第几天  
    	        int week = now.get(Calendar.DAY_OF_WEEK); 
    	        // 打印日历头部标示  
    	        System.out.println("日	一	二	三	四	五	六");
    	        // 打印当前日期前面的空格  
    	        for (int i = Calendar.SUNDAY; i < week; i++) { 
    	            System.out.print("	");  
    	        } 
    	        // 打印日历主体  
    	        while (now.get(Calendar.MONTH) == month) { 
    	            int day = now.get(Calendar.DAY_OF_MONTH); 
    	            // 对输出的日历进行对齐,小于10的加上一个空格  
    	            if (day < 10) { 
    	                // 如果是当前日期,加上标示  
    	                if (day == toDay) { 
    	                    System.out.print("▲" + day + "▲	"); 
    	                } else { 
    	                    System.out.print(" " + day + "	"); 
    	                } 
    	            } else { 
    	                // 如果是当前日期,加上标示  
    	                if (day == toDay) { 
    	                    System.out.print("▲" + day + "▲	"); 
    	                } else { 
    	                    System.out.print("" + day + "	"); 
    	                } 
    	            }
    	            //如果是周六,进行换行  
    	            if (week == Calendar.SATURDAY) { 
    	                System.out.println(); 
    	            } 
    	            //每次输出日期后,将日期增加一天  
    	            now.add(Calendar.DAY_OF_MONTH, 1); 
    	            //重新获得一周的第几天  
    	            week = now.get(Calendar.DAY_OF_WEEK);  
    	        }
    	    }
    	    
    }
    


  • 相关阅读:
    Linux之安装python3.6.6
    Python之计算器
    springboot项目快速代码生成工具
    电脑忘记密码怎么办?
    HTML编辑器
    WCF的几种寄宿方式
    获取客户端IP 和 外网IP
    发送短信验证码
    动态库,服务tips
    asp.net WebService 与 WCF的区别
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3163138.html
Copyright © 2011-2022 走看看