자바 달력 인쇄 방법

1412 단어 일력
package cn.baokx.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Test {
	public static void main(String ... args) {
		String date_str = "2015-04-10";
		getMyCalendar(date_str);
	}

	private static void getMyCalendar(String date_str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = sdf.parse(date_str);
			Calendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			//          
			int day = calendar.get(Calendar.DATE);
			//       1 
			calendar.set(Calendar.DATE, 1);
			//    1       
			int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
			//            
			int lastDay = calendar.getActualMaximum(Calendar.DATE);
			//      6 7       
			int [] days = new int[6*7];
			//      
			for(int i=1 ; i <= lastDay ; i++){
				days[i+(firstDayOfWeek-1)-1] = i;
			}
			//    
			System.out.println(" \t \t \t \t \t \t ");
			for(int i = 0 ; i < days.length ; i++){
				if(days[i]!=0){
					if(days[i]==day){
						System.out.print("*");
					}
					System.out.print(days[i]);
				}
				System.out.print("\t");
				if((i+1)%7==0){
					System.out.println("");
				}
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
	}
}

좋은 웹페이지 즐겨찾기