日期:2014-05-20 浏览次数:20937 次
public static void main(String[] args) { String dateStr="2012-9-8"; SimpleDateFormat sdf=new SimpleDateFormat("yy-MM-dd"); Date date=null; try { date=sdf.parse(dateStr); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } Calendar cal=Calendar.getInstance(); cal.setTime(date); System.out.println(cal.DAY_OF_MONTH); System.out.println(cal.DAY_OF_YEAR); System.out.println(cal.WEEK_OF_MONTH); cal.add(cal.DATE, 7); System.out.println("========我是无耻的分割线========="); System.out.println(cal.DAY_OF_MONTH); System.out.println(cal.DAY_OF_YEAR); System.out.println(cal.WEEK_OF_MONTH); }
5 6 4 ========我是无耻的分割线========= 5 6 4
cal.setTime(date); System.out.println(cal.get(Calendar.DAY_OF_WEEK)); System.out.println(cal.get(Calendar.DAY_OF_YEAR)); System.out.println(cal.get(Calendar.WEEK_OF_MONTH)); cal.add(Calendar.DATE, 7); System.out.println("========我是无耻的分割线========="); System.out.println(cal.get(Calendar.DAY_OF_WEEK)); System.out.println(cal.get(Calendar.DAY_OF_YEAR)); System.out.println(cal.get(Calendar.WEEK_OF_MONTH));
------解决方案--------------------
System.out.println(cal.get(5));
System.out.println(cal.get(6));
System.out.println(cal.get(4));
和
System.out.println(cal.get(Calendar.DAY_OF_WEEK));
System.out.println(cal.get(Calendar.DAY_OF_YEAR));
System.out.println(cal.get(Calendar.WEEK_OF_MONTH));
是一样的结果,你最初拿到的是calendar里面的field值
------解决方案--------------------
楼上正确:
修改后输出:
8
252
2
========我是无耻的分割线=========
15
259
3
------解决方案--------------------
楼上正解。cal.DAY_OF_WEEK等一系列方法都是返回对应的字段的数值,需要使用get方法才能获取到具体的值。意思就是说,DAY_OF_WEEK,DAY_OF_YEAR,WEEK_OF_MONTH这些字段在Calendar类中都有对应的值,通过该值可以获取到相应的值。