我想获得给定周数的日期。对于实例,如果我的周数为15,我需要获取日期: 05-04-2015,06-04-2015,07-04-2015,08-04-2015,09-04-2015 ,10-04-2015,11-04-2015 。
这可能吗?如果是这样,怎么样?
让我知道,
谢谢!
答案 0 :(得分:2)
int yourYear = 1997;
Calendar c1 = Calendar.getInstance();
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
c1.set(yourYear, 1, 1);
c1.add(Calendar.DATE, WEEK*7);
for (int i = 0, i < 7; i++){
String formatted = format1.format(cal.getTime());
System.out.println(formatted);
c1.add(Calendar.DATE, 1);
}
答案 1 :(得分:1)
public static void main(String [] args){
// set the date
Calendar cal = Calendar.getInstance();
cal.set(2011, 10 - 1, 12);
// "calculate" the start date of the week
Calendar first = (Calendar) cal.clone();
first.add(Calendar.DAY_OF_WEEK,
first.getFirstDayOfWeek() - first.get(Calendar.DAY_OF_WEEK));
// and add six days to the end date
Calendar last = (Calendar) first.clone();
last.add(Calendar.DAY_OF_YEAR, 6);
// print the result
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(df.format(first.getTime()) + " -> " +
df.format(last.getTime()));
}
答案 2 :(得分:1)
这是我的解决方案:
Calendar c = new GregorianCalendar(Locale.getDefault());
c.set(Calendar.WEEK_OF_YEAR, 15);
c.set(Calendar.YEAR, 2015);
String result = "";
int firstDayOfWeek = c.getFirstDayOfWeek();
for (int i = firstDayOfWeek; i < firstDayOfWeek + 7; i++) {
c.set(Calendar.DAY_OF_WEEK, i);
result += new SimpleDateFormat("yyyy.MM.dd").format(c.getTime()) + "\n";
}
作为输入,除了一周,你需要有一年。 请注意,一周的第一天取决于区域设置。