显示本月日历demo

时间:2023-07-21 22:53:26
显示本月日历demo
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.GregorianCalendar; public class TestCalendar {
public static void main(String[] args) {
GregorianCalendar now=new GregorianCalendar();
int month=now.get(Calendar.MONTH);
int today=now.get(Calendar.DAY_OF_MONTH);
int intent=0;
now.set(Calendar.DAY_OF_MONTH, 1);
int week=now.get(Calendar.DAY_OF_WEEK);
int firstDayOfWeek=now.getFirstDayOfWeek();
while(firstDayOfWeek!=week){
++intent;
now.add(Calendar.DAY_OF_MONTH, -1);
week=now.get(Calendar.DAY_OF_WEEK);
}
String[] weekdayName=new DateFormatSymbols().getShortWeekdays();
do{
System.out.printf("%4s",weekdayName[week]);
now.add(Calendar.DAY_OF_MONTH, 1);
week=now.get(Calendar.DAY_OF_WEEK);
}while(week!=firstDayOfWeek);
System.out.println();
for(int i=0;i<intent;i++){
System.out.println(" ");
}
now.set(Calendar.DAY_OF_MONTH, 1);
week=now.get(Calendar.DAY_OF_WEEK);
int day=now.get(Calendar.DAY_OF_MONTH);
do{
System.out.printf("%4s", day);
if(today==day){
System.out.print("*");
}else {
System.out.print(" ");
}
now.add(Calendar.DAY_OF_MONTH, 1);
week=now.get(Calendar.DAY_OF_WEEK);
day=now.get(Calendar.DAY_OF_MONTH);
if(week==firstDayOfWeek){
System.out.println();
}
}while(month==now.get(Calendar.MONTH));
if(week!=firstDayOfWeek){
System.out.println();
}
}
}