我正在使用一种从给定日期字符串计算下周一的方法。
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 年?
更新:Joda-Time 项目现在处于维护模式,团队建议迁移到 java.time 类。这个答案作为历史完好无损地留下了。请参阅我的新答案。
乔达时间
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
在组合日期和日历时有些奇怪,在解析日期时,仅使用日历效果很好;
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。 (日历.一月 == 0)//true
tl;dr
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 及更高版本中。这些类取代了麻烦的旧传统日期时间类,如java.util.Date
、Calendar
和SimpleDateFormat
。
Joda-Time 项目现在处于维护模式,建议迁移到 java.time 类。
要了解更多信息,请参阅 Oracle 教程。并搜索堆栈溢出以获取许多示例和解释。规范为 JSR 310。
从哪里获得java.time类?
- Java SE
- 8、Java SE 9 及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9添加了一些小功能和修复。
Java SE - 6 和 Java SE 7
- 许多java.time功能在ThreeTen-Backport中向后移植到Java 6和7。
- 人造人
- java.time 类的 Android 捆绑包实现的更高版本。
- 对于早期的Android,ThreeTenABP项目适应了ThreeTen-Backport(如上所述)。请参阅如何使用ThreeTenABP...。
ThreeTen-Extra项目通过额外的类扩展了java.time。这个项目是未来可能添加到java.time的试验场。你可以在这里找到一些有用的类,如Interval
、YearWeek
、YearQuarter
等。