SimpleDateFormat周计算 [英] SimpleDateFormat Week Calculations

查看:176
本文介绍了SimpleDateFormat周计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SimpleDateFormat获得一些令人费解的结果,希望有人能够解决这个问题。输出:

 时间=星期一12月27日00:00:00 PST 2010 
2010-01< - - 这是我不理解
开始星期=太阳12月26日00:00:00 PST 2010
2010-01
周末结束=星期六1月01 23:59:59 PST 2011
2011-01

我应该在一年的最后一个星期作为特殊情况延伸到明年?或者这是解释这个的正确方法吗?显然,当试图按顺序组织一周时,顺序不正确。调整初始值,2005年12月25日被认为是第53周。我没有看过Joda,看看Joda是否产生类似的结果。



相关代码:

  private static Date getStartOfWeek(Date d){
日历calendar = Calendar.getInstance();
calendar.clear();
calendar.setTime(d);

calendar.set(Calendar.DAY_OF_WEEK,calendar.getFirstDayOfWeek());

return calendar.getTime();
}

private static Date getEndOfWeek(Date d){
日历calendar = Calendar.getInstance();
calendar.clear();

calendar.setTime(d);
calendar.add(Calendar.WEEK_OF_YEAR,1);
calendar.set(Calendar.DAY_OF_WEEK,calendar.getFirstDayOfWeek());
calendar.add(Calendar.MILLISECOND,-1);

return calendar.getTime();
}


日历calendar = Calendar.getInstance();
calendar.clear();
calendar.set(2010,Calendar.DECEMBER,27);
日期d = calendar.getTime();
Date start = getStartOfWeek(d);
日期结束= getEndOfWeek(d);
SimpleDateFormat fmt = new SimpleDateFormat(yyyy-ww);

System.out.println(Time =+ d);
System.out.println(fmt.format(d));
System.out.println(Start of week =+ start);
System.out.println(fmt.format(start));
System.out.println(End of Week =+ end);
System.out.println(fmt.format(end));

背景:在JasperReports中使用交叉表(按周分组的日期)时,我发现这一点。 >

编辑:我正在使用JDK 1.6.0_25



编辑:似乎我将不得不使用Joda来获取正确的结果。要得到一周的开始/结束,我最终使用: LocalDate.withDayOfWeek 。要检索年份和周数,我使用 DateTime.getWeekyear DateTime.getWeekOfWeekyear

解决方案

错误在您的格式化代码,而不是Java。



令人惊讶的行为是以日期符号的深奥规则。请注意,使用周数时,ISO 8601(相当令人困惑)指定了年限的不同规则。特别是,2010-12-27被认为是使用周数的2011年的一部分。



因此,您应该使用周年 YYYY 而不是通常的年份 yyyy 。 (请参阅 http://download.oracle.com/javase/7/ docs / api / java / util / GregorianCalendar.html#week_year http://download.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html 。)



另外,日期的标准符号使用一个明确的'W',所以你应该使用新的SimpleDateFormat(YYYY-W'ww)而不是。



编辑:还有另一个问题。 Java似乎默认为非标准的 calendar.getMinimalDaysInFirstWeek()== 1 ,所以你必须设置

  calendar.setMinimalDaysInFirstWeek(4); 

以获得正确的一年。



编辑:从阅读日历 javadocs,您可能需要将开始日期设置为星期一。此外,Java 1.7中的 YYYY 格式说明符似乎是新的。有鉴于此,除非您愿意升级到预发行Java版本,否则我建议您使用Joda Time。


I'm getting some puzzling results with SimpleDateFormat and am hoping that someone can shed some light on the issue. The output:

Time          = Mon Dec 27 00:00:00 PST 2010
2010-01 <--- THIS IS WHAT I DON'T UNDERSTAND
Start of week = Sun Dec 26 00:00:00 PST 2010
2010-01
End of Week   = Sat Jan 01 23:59:59 PST 2011
2011-01

Should I be treating the last "week" of the year that extends to the next year as a special case? Or is this the correct way to interpret this? Obviously when attempting to organize week sequentially, the order is incorrect. Adjusting the initial values, Dec 25, 2005 is considered the 53rd week. I haven't looked at Joda yet to see if Joda produces similar results.

The relevant code:

private static Date getStartOfWeek( Date d ) {
  Calendar calendar = Calendar.getInstance();
  calendar.clear();
  calendar.setTime( d );

  calendar.set( Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() );

  return calendar.getTime();  
}

private static Date getEndOfWeek( Date d ) {
  Calendar calendar = Calendar.getInstance();
  calendar.clear();

  calendar.setTime( d );
  calendar.add( Calendar.WEEK_OF_YEAR, 1 );
  calendar.set( Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() );
  calendar.add( Calendar.MILLISECOND, -1 );

  return calendar.getTime();
}


Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set( 2010, Calendar.DECEMBER, 27 );
Date d = calendar.getTime();
Date start = getStartOfWeek( d );
Date end = getEndOfWeek( d );
SimpleDateFormat fmt = new SimpleDateFormat( "yyyy-ww" );

System.out.println( "Time          = " + d );
System.out.println( fmt.format( d ) );
System.out.println( "Start of week = " + start );
System.out.println( fmt.format( start ) );
System.out.println( "End of Week   = " + end );
System.out.println( fmt.format( end ) );

Background: I found this when using the crosstab (date grouped into week) in JasperReports.

EDIT: I am using JDK 1.6.0_25

EDIT: It seems that I will have to use Joda to get the correct result. TO get the week start/end, I ended up using: LocalDate.withDayOfWeek. To retrieve the year and week number, I used DateTime.getWeekyear and DateTime.getWeekOfWeekyear.

解决方案

The bug is in your formatting code, not Java.

The surprising behavior is due to an esoteric rule in date notation. Note that ISO 8601 (rather confusingly) specifies different rules for year boundaries when using week numbers. In particular, 2010-12-27 is considered part of 2011 when using week numbers.

As a result, you should be using the "week year" YYYY rather than the usual year yyyy. (See http://download.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#week_year and the last example in http://download.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html.)

Also, the standard notation for dates uses an explicit 'W', so you should use new SimpleDateFormat( "YYYY-'W'ww" ) instead.

Edit: There's another problem. Java seems to default to the non-standard calendar.getMinimalDaysInFirstWeek() == 1, so you have to set

calendar.setMinimalDaysInFirstWeek( 4 );

in order to get the correct year.

Edit: From reading the Calendar javadocs, you might also need to set the starting day to Monday. Further, the YYYY format specifier seems to be new in Java 1.7. In light of this, unless you're willing to upgrade to a pre-release Java version, I recommend just using Joda Time.

这篇关于SimpleDateFormat周计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆