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);  
	        }
	    }
	    
}


原文地址:https://www.cnblogs.com/jiangu66/p/3163138.html