请求前辈讲解一下下面的代码,不停地set (day_of_month,1)是为什么呢?代码不太看的懂,希望有人能讲解一下,谢谢了:
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class T2{
public static void main(String[] args) throws ParseException{
Date date=new Date();
DateFormat format=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
System.out.println("北京时间:"+format.format(date));
Calendar cal=Calendar.getInstance();
int year=cal.get(Calendar.YEAR);
int month=cal.get(Calendar.MONTH);
int today = cal.get(Calendar.DAY_OF_MONTH);
int count=0;
//cal.set(Calendar.DAY_OF_MONTH, );
//System.out.println(cal.get(Calendar.DAY_OF_MONTH));
cal.set(Calendar.DAY_OF_MONTH,1);
int week = cal.get(Calendar.DAY_OF_WEEK);
//System.out.println(cal.get(Calendar.DAY_OF_WEEK));
int firstDayOfWeek = cal.getFirstDayOfWeek();
while(firstDayOfWeek != week){
++count;
cal.add(Calendar.DAY_OF_MONTH, -1);
week = cal.get(Calendar.DAY_OF_WEEK);
}
String[] weeks = { "日", "一", "二", "三", "四", "五", "六" };
for(String w:weeks){
System.err.print(w+" ");
}
do{
cal.add(Calendar.DAY_OF_MONTH, 1);
week = cal.get(Calendar.DAY_OF_WEEK);
}while(week != firstDayOfWeek);
System.out.println();
for(int i = 0;i < count;++i){
System.out.print(" ");
}
cal.set(Calendar.DAY_OF_MONTH, 1);
week = cal.get(Calendar.DAY_OF_WEEK);
int day = cal.get(Calendar.DAY_OF_MONTH);
//System.out.println(day);
do{
System.out.printf("%2s",day);
if(today == day){
System.out.print("*");
}else{
System.out.print(" ");
}
cal.add(Calendar.DAY_OF_MONTH,1 );
week = cal.get(Calendar.DAY_OF_WEEK);
day = cal.get(Calendar.DAY_OF_MONTH);
if(week == firstDayOfWeek){
System.out.println();
}
}while(month == cal.get(Calendar.MONTH));
if(week != firstDayOfWeek){
System.out.println();
}
}
}
添加回答
举报
0/150
提交
取消