今天是2014-04-06 – SUNDAY.
输出使用以下代码是: –
开始日期= 2014-04-07
结束日期= 2014-04-13
我想输出为: –
开始日期= 2014-03-31
结束日期= 2014-04-06
// Get calendar set to current date and time Calendar c = GregorianCalendar.getInstance(); System.out.println("Current week = " + Calendar.DAY_OF_WEEK); // Set the calendar to monday of the current week c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY); System.out.println("Current week = " + Calendar.DAY_OF_WEEK); // Print dates of the current week starting on Monday DateFormat df = new SimpleDateFormat("yyyy-MM-dd",Locale.getDefault()); String startDate = "",endDate = ""; startDate = df.format(c.getTime()); c.add(Calendar.DATE,6); endDate = df.format(c.getTime()); System.out.println("Start Date = " + startDate); System.out.println("End Date = " + endDate);
解决方法
我相信只需使用:
c.setFirstDayOfWeek(Calendar.MONDAY);
会解决你的问题…
总结
以上是DEVMAX为你收集整理的在Java中获取当前周的开始和结束日期 – (MONDAY TO SUNDAY)全部内容。
如果觉得DEVMAX网站内容还不错,欢迎将DEVMAX网站推荐给好友。