论坛首页 Java版

如何确定一个月的某一周是从几月几号到几号

浏览 2404 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2006-01-06
比如说,我想知道2005年9月份的第二周,是从哪天到哪天的?答案是从2005-09-04到2005-09-10
请高手给我点儿思路,我应该从哪儿去想这个问题
   
时间:2006-01-06
java.util.Calendar里有丰富的周数计算方法,你去参考一下就知道叻。

不过关于WEEK_OF_YEAR,Calendar类有个严重的Bug。比如2005年12月31日是星期六,2006年1月1日是星期日,那么,按照星期一是每周的第一天来算,2005年12月31日这一天,是2005年的最后一周,是53周,而当你调用calendar.get(Calendar.WEEK_OF_YEAR)时,它会返回1,也即它是按照2006年来算的,而如果你再去calendar.set(Calendar.WEEK_OF_YEAR, 1),那么日期就变成2005年1月的某一天叻。

如果这个不算Calendar的bug的话,也应该算做是一个非常需要注意的地方。
   
0 请登录后投票
时间:2006-01-06
hongliang 写道

不过关于WEEK_OF_YEAR,Calendar类有个严重的Bug。比如2005年12月31日是星期六,2006年1月1日是星期日,那么,按照星期一是每周的第一天来算,2005年12月31日这一天,是2005年的最后一周,是53周,而当你调用calendar.get(Calendar.WEEK_OF_YEAR)时,它会返回1,也即它是按照2006年来算的,而如果你再去calendar.set(Calendar.WEEK_OF_YEAR, 1),那么日期就变成2005年1月的某一天叻。


有这么怪异的事?你用的JDK什么版本?

[code:1]
Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat("yyyyMMdd").parse("20051231"));
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));
[/code:1]

System.out 写道
53
   
0 请登录后投票
时间:2006-01-06
jkit 写道

有这么怪异的事?你用的JDK什么版本?
[code:1]
Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat("yyyyMMdd").parse("20051231"));
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));
[/code:1]
System.out 写道
53


在SUN JDK 1.4.2-10上,你可以试着执行这样一段代码:

[code:1]
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(new SimpleDateFormat("yyyyMMdd").parse("20051231"));
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));
[/code:1]

你那段之所以输出53是因为本来去年的12月31日就是最后一周的最后一天(按照星期天是第一天,星期六是最后一天算),如果指定Calendar按照第一天为星期一来算周的话,就会有我说的那个问题

刚才在BEA JRockit 5.0上也试叻一下,问题依旧。不知道这里面是不是有什么标准,而跟我们的习惯不太相同?
   
0 请登录后投票
时间:2006-01-06
确实有这个问题,不过,看代码里面的注释,似乎是故意这样的。
   
0 请登录后投票
时间:2006-01-06
是啊,这个问题12月31号那天搞的我狠被动,最后发现可以这样解决:

[code:1]
calendar.getActualMaximum(Calendar.WEEK_OF_YEAR);
[/code:1]


   
0 请登录后投票
时间:2006-01-09
Date mDate=null;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");
try{
mDate=sdf.parse("2005-11-10");
}catch(java.text.ParseException pe){}

Calendar cld=Calendar.getInstance();
cld.setFirstDayOfWeek(Calendar.MONDAY);
cld.setTime(mDate);
int i=cld.get(Calendar.WEEK_OF_MONTH);
System.out.println("this is the "+i+" week!");

DateFormat dateFormat =
DateFormat.getDateInstance(DateFormat.FULL);
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(mDate);
System.out.println("System Date: " +
dateFormat.format(cal.getTime()));

怎么会这样呢?2005-11-10应该是第2个星期才对呢,结果却是第三个星期;而且System Date: 2005年1月10日 星期一
而不是System Date: 2005年11月10日 星期四!
不论是2005-11-10还是2005-12-10都会自动转换成2005-01-10!
   
0 请登录后投票
时间:2006-01-09
[code:1]
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
[/code:1]

真不知道说楼上的什么好。。。
   
0 请登录后投票
时间:2006-01-09

这样的话就对了!
int week=3;
Date mDate=null;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
try{
mDate=sdf.parse("2006-02-01");
}catch(java.text.ParseException pe){}

Calendar cld=Calendar.getInstance();
cld.setFirstDayOfWeek(Calendar.MONDAY);
cld.setTime(mDate);
int i=cld.get(Calendar.WEEK_OF_MONTH);
System.out.println("this is the "+i+" week!");

DateFormat dateFormat =
DateFormat.getDateInstance(DateFormat.FULL);
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(mDate);
System.out.println("System Date: " +
dateFormat.format(cal.getTime()));

cal.set(GregorianCalendar.DAY_OF_WEEK,
GregorianCalendar.MONDAY);
System.out.println("After Setting Day of Week to Monday: " +
dateFormat.format(cal.getTime()));

cal.add(GregorianCalendar.DAY_OF_MONTH, 7*(week-1));
System.out.println("第三周的星期一是"+dateFormat.format(cal.getTime()));
cal.add(GregorianCalendar.DAY_OF_MONTH, 6);
System.out.println("第三周的星期日是"+dateFormat.format(cal.getTime()));
这样的话我就可以推出任何一个月的任何一个星期的开始和结束了!
呵呵!小女子在此谢过各位了!
   
0 请登录后投票
论坛首页 Java版

跳转论坛: