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("日\t一\t二\t三\t四\t五\t六");
	        // 打印当前日期前面的空格  
	        for (int i = Calendar.SUNDAY; i < week; i++) { 
	            System.out.print("\t");  
	        } 
	        // 打印日历主体  
	        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 + "▲\t"); 
	                } else { 
	                    System.out.print(" " + day + "\t"); 
	                } 
	            } else { 
	                // 如果是当前日期,加上标示  
	                if (day == toDay) { 
	                    System.out.print("▲" + day + "▲\t"); 
	                } else { 
	                    System.out.print("" + day + "\t"); 
	                } 
	            }
	            //如果是周六,进行换行  
	            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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值