我使用的是從給定日期字符串計算在下週一的方法。問題與日曆計算跨越2歷年
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」,但一年「2014」,所以輸出在技術上是正確的。我的目的錯了。
如何將我最好的告訴大家,我想在1周下週一日曆對象,但2015年?
可能重複[獲取特定日期後的第一個星期一?](http://stackoverflow.com/questions/7565356/get-first-monday-after-certain-date)和[this](http:// stackoverflow .com/q/9612328/642706)和[this](http://stackoverflow.com/q/10072914/642706)和[this](http://stackoverflow.com/q/26754164/642706)以及其他許多人。提示:Joda-Time。 – 2014-11-24 17:35:41
您的輸出中的月份也是錯誤的,而不僅僅是一年。 2014-12-29之後的第一個星期一是2015-01-05(不是第6個)。 – 2014-11-25 07:10:05
羅勒,仔細觀察 - 從2014年12月29日起,它將於下週一返回6.1。** 2014 **。2014年,其實**是**不正確。 – 2014-11-25 10:27:58