【问题标题】:Issue with Calendar calculation that spans 2 calendar years跨越 2 个日历年的日历计算问题
【发布时间】:2014-11-24 14:48:58
【问题描述】:

我正在使用一种根据给定日期字符串计算下周一的方法。

public static String getStartOfNextWeek(String DATE){

String format = "dd.MM.yyyy";SimpleDateFormat df = new SimpleDateFormat(format);

Date date = null;
try {
date = df.parse(DATE);
} catch (ParseException e) {
e.printStackTrace();
}

Calendar cal = Calendar.getInstance();

cal.setTime(date);
int week = cal.get(Calendar.WEEK_OF_YEAR);
int year = cal.get(Calendar.YEAR);

Calendar calendar = new GregorianCalendar();
calendar.clear(); 
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.WEEK_OF_YEAR, week);

//add 8 days to get next weeks Monday

calendar.add(Calendar.DAY_OF_WEEK, 8);

Date startDate = calendar.getTime();

SimpleDateFormat df2 = new SimpleDateFormat("dd.MM.yyyy");

String start = df2.format(startDate);

return start;

这个工作在一个日历年中非常好,但是当我传递一个跨越两个日历年的值时会出现问题。

例如:

input: 15.12.2014
output: 22.12.2014 CORRECT
input: 22.12.2014
output: 29.12.2014 CORRECT
input: 29.12.2014
output: 6.1.2014 INCORRECT

我知道错误的位置,因为它将 WEEK_OF_YEAR 作为“1”,但将 YEAR 作为“2014”,所以输出在技术上是正确的。对我的目的来说是错误的。

我如何最好地告诉日历对象我想要第 1 周的下一个星期一,但 2015 年?

【问题讨论】:

  • Get first Monday after certain date?thisthisthis 和许多其他的可能重复。提示:Joda-Time。
  • 您的输出中的日期也是错误的,而不仅仅是年份。 2014-12-29 之后的第一个星期一是 2015-01-05(不是第 6 个)。
  • Basil,仔细观察 - 它从 29.12.2014 开始返回 6.1.**2014** 作为下周一,这实际上不正确的。

标签: java calendar


【解决方案1】:

更新:Joda-Time 项目现在位于maintenance mode,团队建议迁移到java.time 类。这个答案作为历史原封不动地保留下来。见my newer Answer

乔达时间

Joda-Time 库,2.5 版,得到了正确答案。并且更容易获得。

// Parse input string.
String input = "29.12.2014";
DateTimeFormatter formatter = DateTimeFormat.forPattern( "dd.MM.yyyy" );
LocalDate inputLocalDate = formatter.parseLocalDate( input );

// Find desired Monday.
LocalDate possibleMonday = inputLocalDate.withDayOfWeek( DateTimeConstants.MONDAY ); 

// The possible Monday could be past, present, or future of our input date. Adjust as needed.
LocalDate desiredMonday = null;
if ( possibleMonday.isBefore( inputLocalDate ) || possibleMonday.isEqual( inputLocalDate ) ) {
    desiredMonday = possibleMonday.plusWeeks( 1 ); // If the possible Monday is past or present, add a week to get *next* Monday.
} else {
    desiredMonday = possibleMonday;  // If the possible Monday is future, use it.
}

String output = formatter.print( desiredMonday ); 

转储到控制台。

System.out.println( "input : " + input );
System.out.println( "inputLocalDate : " + inputLocalDate );
System.out.println( "desiredMonday : " + desiredMonday );
System.out.println( "output : " + output );

运行时。

input : 29.12.2014
inputLocalDate : 2014-12-29
desiredMonday : 2015-01-05
output : 05.01.2015

【讨论】:

  • 太好了,谢谢,正是我想要的。简单而全面。
【解决方案2】:

结合 Date 和 Calender 有一些奇怪的地方,在解析日期时,只使用 Calender 效果很好;

    String[] dt = dateStr.split("\\.");
    GregorianCalendar cal = new GregorianCalendar(Integer.parseInt(dt[2]), (Integer.parseInt(dt[1])-1), Integer.parseInt(dt[0]));
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    cal.clear(Calendar.DAY_OF_WEEK);
    cal.add(Calendar.DATE, 7);
    System.out.println(cal.get(Calendar.YEAR) + "." + (cal.get(Calendar.MONTH)+1) + "." + cal.get(Calendar.DATE));

编辑:您必须从月份中减去 1,因为日历预计月份的范围是 0 到 11。 (Calendar.JANUARY == 0) //true

【讨论】:

    【解决方案3】:

    tl;博士

    LocalDate.parse( 
        "29.12.2014" ,
        DateTimeFormatter.ofPattern( "dd.MM.uuuu" )
    ).with(
        TemporalAdjusters.next( DayOfWeek.MONDAY )
    )
    

    2015-01-05

    java.time

    现代方法使用 java.time 类。

    DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd.MM.uuuu" )
    LocalDate ld = LocalDate.parse( "29.12.2014" , f ) ;
    

    要移动到另一个日期,请使用 TemporalAdjusters 类中的 TemporalAdjuster 实现。使用 DayOfWeek 枚举对象指定所需的星期几。跨过年末/年初也没问题。

    LocalDate followingMonday = ld.with( TemporalAdjusters.next( DayOfWeek.MONDAY ) ) ;
    

    如果您想使用当前日期(如果是星期一),请使用TemporalAdjusters.nextOrSame

    类似的方法也提供了上一个星期几:previous & previousOrSame


    关于java.time

    java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

    Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

    要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310

    从哪里获得 java.time 类?

    ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      相关资源
      最近更新 更多