java 简单的日历(Calendar)问题
package com.syxy;import java.util.*;
public class Rili{
public static void main(String[] args){
GregorianCalendar now =new GregorianCalendar();
Date date = now.getTime();
System.out.println(date.toString());
now.setTime(date);
int today =now.get(Calendar.DAY_OF_MONTH);
int month =now.get(Calendar.MONTH);
now.set(Calendar.DAY_OF_MONTH,1);
int week=now.get(Calendar.DAY_OF_WEEK);
System.out.println(" 星期日 星期一 星期二 星期三 星期四 星期五 星期六");
for(int i=Calendar.SUNDAY;i< week;i++)
System.out.print(" ");
while (now.get(Calendar.MONTH) == month )
{
int day =now.get(Calendar.DAY_OF_MONTH);
if(day < 10){
if(day==today)
System.out.print(" <" + day +"> ");
else
System.out.print(" " + day + " ");
} else {
if( day== today)
System.out.print(" <" + day +"> ");
else
System.out.print(" " + day + " " );
}
if (week== Calendar.SATURDAY){
System.out.println();
}
now.add(Calendar.DAY_OF_MONTH,1);
week=now.get(Calendar.DAY_OF_WEEK);
}
}
}
//day 与today 不是一直相等的吗? java --------------------编程问答-------------------- 楼主这代码,能编译过么 --------------------编程问答-------------------- 我写了一个测试类,day是变化的
public class Test
{
public static void main(String[] args)
{
GregorianCalendar now = new GregorianCalendar();
// System.out.println(now.get(Calendar.MONTH));
//
// System.out.println(now.get(Calendar.DAY_OF_MONTH));
//
// System.out.println(Calendar.DAY_OF_MONTH);
for (int i=0; i<30; i++)
{
int day = now.get(Calendar.DAY_OF_MONTH);
System.out.println(day);
now.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
原因在add方法,具体为什么你可以看源代码,源代码太多了,看得我头晕,在add方法的源代码实现中有答案
补充:Java , Java SE