本文借鉴《JAVA核心开发:卷2》
Date类有点过时,但其中的new Date().toString()比较常用,而且非常好用,比起Clendar.toString()好多了。
好,步入正题,下面就是本年日历的程序:
/**@authorHUI_土疙瘩*/
importjava.util.*;
public class CalendarTest {
public static void main(String[] args) {
GregorianCalendar date = new GregorianCalendar();
//获得今天
int today = date.get(Calendar.DAY_OF_MONTH);
//获得当今月份
int nowMonth = date.get(Calendar.MONTH);
//设置月份从一月一号开始
date.set(Calendar.DAY_OF_MONTH, 1);
date.set(Calendar.MONTH, 0);
date.setFirstDayOfWeek(Calendar.SUNDAY);
for(int i=Calendar.JANUARY; i<=Calendar.DECEMBER; i++){
int weekday = date.get(Calendar.DAY_OF_WEEK);
int month = date.get(Calendar.MONTH);
System.out.println(" Month:" + (month+1));
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
for(int j=Calendar.SUNDAY; j<weekday; j++)
System.out.print(" ");
do{
int day = date.get(Calendar.DAY_OF_MONTH);
System.out.printf("%3d", day);
if(day == today && month == nowMonth)
System.out.print("#");
else
System.out.print(" ");
if(date.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
System.out.println();
date.add(Calendar.DAY_OF_MONTH, 1);
}while(month == date.get(Calendar.MONTH));
//换行
if(date.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY)
System.out.println();
System.out.println();
}//for
}
}
PS:小弟刚刚入道,欢迎各位大虾指正批评。