这让我疯了!
我到处搜索过,例如找到了这个问题:How to get dates of a week (I know week number)?
但是我无法在我的实现中使用它(在NEXUS 7上的ANDROID API19上):
public Pair<String,String> getWeekRange(int year, int week_no) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.WEEK_OF_YEAR, week_no);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
Date monday = cal.getTime();
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
Date sunday = cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
return new Pair<String,String>(sdf.format(monday), sdf.format(sunday));
}
对于getWeekRange(2014, WEEK)
的任何通话,它总是给我第2周的日期范围(恰好是今天的当前周数)??
日历似乎是一个非常令人困惑的课程,必须有一些我不理解的东西。但是根据文档,YEAR
,WEEK_OF_YEAR
和DAY_OF_WEEK
应该足以让我设置。
Some test data:
01-10 18:28:54.730: V/JSON(24503): Week 1/2014 is date range:2014-01-06 to 2014-01-12
01-10 18:28:54.730: V/JSON(24503): Week 2/2014 is date range:2014-01-06 to 2014-01-12
01-10 18:28:54.730: V/JSON(24503): Week 3/2014 is date range:2014-01-06 to 2014-01-12
01-10 18:28:54.730: V/JSON(24503): Week 4/2014 is date range:2014-01-06 to 2014-01-12
01-10 18:28:54.740: V/JSON(24503): Week 5/2014 is date range:2014-01-06 to 2014-01-12
01-10 18:28:54.740: V/JSON(24503): Week 1/2013 is date range:2013-01-07 to 2013-01-13
01-10 18:28:54.740: V/JSON(24503): Week 2/2013 is date range:2013-01-07 to 2013-01-13
01-10 18:28:54.740: V/JSON(24503): Week 3/2013 is date range:2013-01-07 to 2013-01-13
01-10 18:28:54.740: V/JSON(24503): Week 4/2013 is date range:2013-01-07 to 2013-01-13
01-10 18:28:54.740: V/JSON(24503): Week 5/2013 is date range:2013-01-07 to 2013-01-13
任何人都可以看到我的错误吗?任何帮助表示赞赏。
**编辑**
似乎代码在Windows上正常运行,这是一个孤立的Android问题。
答案 0 :(得分:3)
写这个问题让我再一次阅读文档,只是在预感中我交换了顺序,所以函数现在看起来像这样:
public Pair<String,String> getWeekRange(int year, int week_no) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.WEEK_OF_YEAR, week_no);
Date monday = cal.getTime();
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.WEEK_OF_YEAR, week_no);
Date sunday = cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
return new Pair<String,String>(sdf.format(monday), sdf.format(sunday));
}
瞧,现在它给了我正确的日期......它必须是日历的内部实现,我不明白。
仍然不知道冗余线是否必要,但现在我很高兴它有效!
答案 1 :(得分:2)
我认为使用Joda-Time进行此类操作要好得多:
public Pair<String,String> getWeekRange(int year, int week_no) {
DateTime startOfWeek = new DateTime().withYear(year).withWeekOfWeekyear(week_no);
DateTime endOfWeek = startOfWeek.plusDays(6);
return new Pair<String,String>(startOfWeek.toString(), endOfWeek.toString());
}
答案 2 :(得分:1)
在不导入Pair类
的情况下执行此操作public void getWeeksRangesDates(int y, int w) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, Calendar.MONDAY);
cal.set(Calendar.YEAR, y);
cal.set(Calendar.WEEK_OF_YEAR, w);
Date date1 = cal.getTime();
cal.add(Calendar.DATE, 6);
Date date2 = cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
Log.v("date1",sdf.format(date1));
Log.v("date2", sdf.format(date2));
}