日历计算的问题跨越2个日历年

时间:2014-11-24 14:48:58

标签: java calendar

我正在使用一种从给定日期字符串计算下一个星期一的方法。

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年?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

组合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

答案 2 :(得分:0)

TL;博士

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

2015年1月5日

java.time

现代方法使用 java.time 类。

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

要移至其他日期,请使用TemporalAdjuster课程中的TemporalAdjusters实施。使用DayOfWeek枚举对象指定所需的星期几。没有问题跨越年末/年初。

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

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

类似的方法也提供之前的星期几:previous& previousOrSame

关于 java.time

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

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

要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310

从哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如IntervalYearWeekYearQuartermore