GregorianCalendar打印日历

  • Post author:
  • Post category:其他


在Java核心书中发现GregorianCalendar类,对日期处理,感觉不错

GregorianCalendar gre = new GregorianCalendar();
		int today = gre.get(Calendar.DAY_OF_MONTH);
		int month = gre.get(Calendar.MONTH);
		gre.set(Calendar.DAY_OF_MONTH,1);
		int weekDay = gre.get(Calendar.DAY_OF_WEEK);
		System.out.println("Sun  Mon  Tue  Wen  Thu  Fri  Sat");
		for (int i = Calendar.DAY_OF_MONTH; i < weekDay; i++) {
			System.out.println("  ");
		}
		do {
			int day = gre.get(Calendar.DAY_OF_MONTH);
			System.out.printf("%3d",day);
			if(day == today){
				System.out.print("* ");
			}else{
				System.out.print("  ");
			}
			if(weekDay == Calendar.SATURDAY)
				System.out.println();
			gre.add(Calendar.DAY_OF_MONTH, 1);
			weekDay = gre.get(Calendar.DAY_OF_WEEK);
			
		} while (gre.get(Calendar.MONTH)== month);
		
		if(weekDay != Calendar.SUNDAY){
			System.out.println();
		}