日期:2014-05-20 浏览次数:20755 次
import java.text.DateFormatSymbols;
import java.util.*;
public class Test
{
public static void main(String[] args)
{
String[] weekdayNames = new DateFormatSymbols().getWeekdays();
for(int i=1; i<=7; i++)
{
System.out.print(weekdayNames[i]);
System.out.print(" ");
}
System.out.println();
GregorianCalendar calendar = new GregorianCalendar();
int firstDayOfWeek = calendar.getFirstDayOfWeek();
calendar.set(Calendar.DAY_OF_MONTH, 1);
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
int blank = 0;
while(weekday != firstDayOfWeek)
{
blank++;
calendar.add(Calendar.DAY_OF_MONTH,-1);
weekday = calendar.get(Calendar.DAY_OF_MONTH);
}
for(int i=0; i<blank; i++)
{
System.out.print(" ");
}
int month = calendar.get(Calendar.MONTH);
calendar.set(Calendar.DAY_OF_MONTH, 1);
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.printf("%7d", day);
calendar.add(Calendar.DAY_OF_MONTH, 1);
while(calendar.get(Calendar.MONTH) == month)
{
weekday = calendar.get(Calendar.DAY_OF_WEEK);
if(weekday == firstDayOfWeek)
{
System.out.println();
}
day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.printf("%7d", day);
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
}
}