【发布时间】:2015-06-12 23:39:06
【问题描述】:
public static void main(String[] args) {
int week = 1;
int year = 2010;
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.WEEK_OF_YEAR, week);
calendar.set(Calendar.YEAR, year);
Date date = calendar.getTime();
System.out.println(date);
}
如果我输入周、年作为输入,我正在根据我们的桌面日历查找确切的开始和结束日期。
但是上面的代码给出的输出为27th Jan, 2009, Sunday。
我知道这是因为根据美国,一周的默认第一天是星期日,但我需要根据桌面日历 1st Jan, 2010, Friday 作为一周的开始日期
我的要求: 如果我的输入是:
- 周为“1”,
- 月份为“5”,
- 年份为“2015”
我需要:
1st May, 2015 --> as first day of the week
2nd May, 2015 --> as last day of the week
如果我的输入是:
- 周为“1”,
- 月份为“6”,
- 年份为“2015”
我需要:
1st June, 2015 --> as first day of the week
6th June, 2015 --> as last day of the week
谁能帮帮我?
【问题讨论】:
-
您在寻找什么样的星期编号?有很多不同的选择,但您需要真正准确了解要求。
-
你想把星期五作为一周的开始吗?
-
旁注:如果您使用的是 Java SE 8,请考虑使用新的日期和时间 API。否则考虑使用 Joda Time。