求助!关于Calendar类的问题
public class TestCalendar {
public static void main(String[] args){
Calendar cl=Calendar.getInstance();
System.out.print(cl.get(Calendar.YEAR)+ "年 " +cl.get(Calendar.MONTH)+ "月 "+ cl.get(Calendar.DAY_OF_MONTH)+ "日 "+ " ");
System.out.print( "星期 "+cl.get(Calendar.DAY_OF_WEEK_IN_MONTH)+ " ");
System.out.println( "21天以后是: ");
cl.add(Calendar.DAY_OF_YEAR,21);
System.out.print(cl.get(Calendar.YEAR)+ "年 " +cl.get(Calendar.MONTH)+ "月 " + cl.get(Calendar.DAY_OF_MONTH)+ "日 "+ " ");
System.out.print( "星期 "+cl.get(Calendar.DAY_OF_WEEK_IN_MONTH)+ " ");
}
}
这是我的一段代码 结果是
2007年0月17日 星期3
21天以后是:
2007年1月7日 星期1
后来我把 cl.get(Calendar.MONTH) 改成了 (cl.get(Calendar.MONTH)+1) 结果月份是对了 但是星期还是有问题 加了21天 应该还是星期3啊
后来的结果是
2007年1月17日 星期3
21天以后是:
2007年2月7日 星期1
(我的系统时间是2007年1月17日星期3)
麻烦各位指教,谢谢了
------解决方案--------------------楼主对 DAY_OF_WEEK_IN_MONTH 的理解有误,楼主应该用 DAY_OF_WEEK。先照抄 JDK 文档如下:
public static final int DAY_OF_WEEK_IN_MONTH = 8
Field number for get and set indicating the ordinal number of the day of the week within the current month. Together with the DAY_OF_WEEK field, this uniquely specifies a day within a month. Unlike WEEK_OF_MONTH and WEEK_OF_YEAR, this field 's value does not depend on getFirstDayOfWeek() or getMinimalDaysInFirstWeek(). DAY_OF_MONTH 1 through 7 always correspond to DAY_OF_WEEK_IN_MONTH 1; 8 through 14 correspond to DAY_OF_WEEK_IN_MONTH 2, and so on. DAY_OF_WEEK_IN_MONTH 0 indicates the week before DAY_OF_WEEK_IN_MONTH 1. Negative values count back from the end of the month, so the last Sunday of a month is specified as DAY_OF_WEEK = SUNDAY, DAY_OF_WEEK_IN_MONTH = -1. Because negative values count backward they will usually be aligned differently within the month than positive values. For example, if a month has 31 days, DAY_OF_WEEK_IN_MONTH -1 will overlap DAY_OF_WEEK_IN_MONTH 5 and the end of 4.
懂了没?每月1~7号,DAY_OF_WEEK_IN_MONTH 为 1;8~14号,DAY_OF_WEEK_IN_MONTH 为 2,依此类推。
------解决方案--------------------Calendar.DAY_OF_WEEK_IN_MONTH=====〉DAY_OF_WEEK