java获取任意周一周末日期
程序员文章站
2022-05-15 11:24:21
...
已经给你封装好方法了
//调用 传值 nn= -1上周 0本周 1下周 依次类推
String arr1[]=addDay(nn);
//封装的方法
public static String[] addDay(int nn) {
String arr[]=new String[2] ;
Calendar cal = Calendar.getInstance();
//int nn = 1;
String monday;
cal.add(Calendar.DATE, nn*7);
//想周几,这里就传几Calendar.MONDAY(TUESDAY...)
cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
//System.out.println(monday);
//赋值
arr[0]=monday;
String s=monday;
//一周7天
int n=7;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cd = Calendar.getInstance();
cd.setTime(sdf.parse(s));
cd.add(Calendar.DATE, n);//增加一天
//cd.add(Calendar.MONTH, n);//增加一个月
//赋值
arr[1]=sdf.format(cd.getTime());
} catch (Exception e) {
}
return arr;
}