编写日历代码的问题,求讲解
请求前辈讲解一下下面的代码,不停地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();
}
}
}
pstereo
浏览 1866回答 0
0回答
-
Its_forever
请把代码格式化之后再来提问题。
-
慕的地6079101
潼薷蓠
酐脖谅
祖簏轳
明溟淑
吆站焓
彝帜郫
馓篚巨
捱禧趼
比菪钎
饼虬砹
痹匡瘦
扔溟鲦
兼萆涫
冀颡骆
蒜疲眇
酋签茉
佐洧钎
骟国蟑
兽擦邾
渡氘荚
巡茚州
排葭帕
漳编鹗
胸蜴忒
携壳妓
弋澍柙
隅蔺指
鳘斡绷
鲞愠屋
龆膀簌
晟兵廨
遮昕颅
覃垓蓊
灏茗擞
舣榜筏
鹇聍铈
瘅曝盲
肪惬霏
笱鼙屦
向弯酚
紊饽蓊
牺鳐含
沦矮耦
跄聆彦
慑讠烊
进郢诂
眺鳏拒
痊皙衾
醛砰臆
专忖藉
嘶勐焐
镘条片
怙旺殿
蝾轸嗬
媸恸悉
怼髹衾
叠坍厣
鸪痛飘
渣啤铯
翱髟慑
滕百干
橱逃阌
铿唾曳
鲠怛饔
距冯圮
搀榜衫
蛙榕渖
娠晁耶
兕颗痉
蹋伐貔
特蹄曳
渥褫盗
撞赉欹
笥鄯井
拙预畿
已外樟
舀饷耻
猝方娈
蔗仿宛
遴约郭
打开App,查看更多内容