67,513
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) throws ParseException{
List<String> list = getPrevTwoWeeksDays("2010-10-23");
for (String str : list)
System.out.println(str);
}
public static List<String> getPrevTwoWeeksDays(String dateStr) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> list = new ArrayList<String>();
Calendar calendar = Calendar.getInstance();
calendar.setTime(sdf.parse(dateStr));
list.add(dateStr);
int count=14;
while(--count>0){
calendar.add(Calendar.DAY_OF_MONTH,-1);
list.add(sdf.format(calendar.getTime()));
}
Collections.sort(list);
return list;
}
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse("2010-10-23");
List<Date> list = listPrevTwoWeeksDates(date);
for (Date d : list) {
System.out.println(sdf.format(d));
}
}
/**
* 列出前两周的日期
*
* @param date
* @return
*/
public static List<Date> listPrevTwoWeeksDates(Date date) {
List<Date> list = new ArrayList<Date>();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -(2 * 7 - 1));
while (calendar.getTime().compareTo(date) <= 0) {
list.add(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
return list;
}