使用Java 库中的GregorianCalendar类打印日历
代码如下:
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarTest {
/**
* @param args
*/
public static void main(String[] args) {
GregorianCalendar d = new GregorianCalendar();
int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONDAY);
d.set(Calendar.DAY_OF_MONTH,1);
int weekDay = d.get(Calendar.DAY_OF_WEEK);
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
for(int i = Calendar.SUNDAY;i<weekDay;i++){
System.out.print(" ");
}
do{
int day = d.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();
}
d.add(Calendar.DAY_OF_MONTH, 1);
weekDay = d.get(Calendar.DAY_OF_WEEK);
}while(d.get(Calendar.MONTH) == month);
if(weekDay!=Calendar.SUNDAY)
System.out.println();
}
}
以上代码来自java核心技术卷一