本文主要是介绍获取当前周的日期,获取下周日期,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/*** 获取本周的日期* @author ywl*/public static List<String> getWeekDay() {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");List<String> str = new ArrayList<String>();Calendar calendar = Calendar.getInstance();while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {calendar.add(Calendar.DAY_OF_WEEK, -1);}Date[] dates = new Date[7];for (int i = 0; i < 7; i++) {dates[i] = calendar.getTime();calendar.add(Calendar.DATE, 1);}for (Date date : dates) {str.add(sdf.format(date));}return str;}/*** 根据当前周日的日期获取下周的日期* @author ywl* @param day* @return*/public static List<String> getNextWeekDay(String day) {List<String> days = new ArrayList<String>();try {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");Calendar cal = Calendar.getInstance();cal.add(Calendar.DATE, -1);cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);cal.add(Calendar.WEEK_OF_YEAR, 1);System.out.println("当周的星期日日期:"+sdf.format(cal.getTime()));Calendar calendar = Calendar.getInstance();Date time = sdf.parse(day);calendar.setTime(time);//根据日期获取下周日期可修改此行代码//这里使用的是当前周日的日期calendar.add(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);if (1 == calendar.get(Calendar.DAY_OF_WEEK)) {calendar.add(Calendar.DAY_OF_MONTH, -1);}calendar.setFirstDayOfWeek(Calendar.MONDAY);calendar.add(Calendar.DATE,calendar.getFirstDayOfWeek()- calendar.get(Calendar.DAY_OF_WEEK));System.out.println("下周星期一的日期:" + sdf.format(calendar.getTime()));Date[] dates = new Date[7];for (int i = 0; i < 7; i++) {dates[i] = calendar.getTime();calendar.add(Calendar.DATE, 1);}for (Date date : dates) {days.add(sdf.format(date));}} catch (Exception e) {e.printStackTrace();}return days;}public static void main(String[] args) {List<String> days = getWeekDay();//传入当前周的周日List<String> day = getNextWeekDay(days.get(days.size()-1));System.out.println(day);}
这篇关于获取当前周的日期,获取下周日期的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!