java获取某个时间的上周一和周日

2024-12-02 07:38:26
推荐回答(2个)
回答1:

mport java.text.SimpleDateFormat;
import java.util.Calendar;
public class Test{
public static void main(String args[]){
Calendar cal = Calendar.getInstance();
//n为推迟的周数,1本周,-1向前推迟一周,2下周,依次类推
int n = 2;
String monday;
cal.add(Calendar.DATE, n*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);
}
}

回答2:

查看java api 日历类的帮助文档