DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

java programmer from china Nile has posted 6 posts at DZone. View Full User Profile

Java 获取指定日期所在周的第一天

05.21.2009
| 3630 views |
  • submit to reddit
        // 获取指定日期所在周的第一天

      Calendar cal = Calendar.getInstance();
      cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);//这里设置从周一开始,若需要根据系统时区自动获取,则采用下边的方式
      //cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());//
      System.out.println(cal.getTime());

测试结果
Wed Apr 29 10:24:45 CST 2009		Mon Apr 27 10:24:45 CST 2009
Thu Apr 30 10:24:45 CST 2009		Mon Apr 27 10:24:45 CST 2009
Fri May 01 10:24:45 CST 2009		Mon Apr 27 10:24:45 CST 2009
Sat May 02 10:24:45 CST 2009		Mon Apr 27 10:24:45 CST 2009
Sun May 03 10:24:45 CST 2009		Mon May 04 10:24:45 CST 2009
Mon May 04 10:24:45 CST 2009		Mon May 04 10:24:45 CST 2009
Tue May 05 10:24:45 CST 2009		Mon May 04 10:24:45 CST 2009
Wed May 06 10:24:45 CST 2009		Mon May 04 10:24:45 CST 2009
Thu May 07 10:24:45 CST 2009		Mon May 04 10:24:45 CST 2009
Fri May 08 10:24:45 CST 2009		Mon May 04 10:24:45 CST 2009
Sat May 09 10:24:45 CST 2009		Mon May 04 10:24:45 CST 2009