2011-08-14 20:29:28|?次阅读|上传:wustguangh【已有?条评论】发表评论
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd"); java.util.Calendar calendar=java.util.Calendar.getInstance(); calendar.setTime(new java.util.Date()); //取得现在时间 System.out.println(sdf.format(new java.util.Date())); //取得上一个时间 calendar.set(Calendar.MONDAY,calendar.get(Calendar.MONDAY)-1); //取得上一个月的下一天 calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1); System.out.println(sdf.format(calendar.getTime()));
我现在要用到在下拉菜单中循环取出最近一年的yyyymm格式的时间
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyyMM"); java.util.Calendar calendar=java.util.Calendar.getInstance(); calendar.setTime(new java.util.Date()); for (int i = 0 ; i < 12 ;i++){ calendar.set(Calendar.MONDAY,calendar.get(Calendar.MONDAY)-1); System.out.println("sdf========="+sdf.format(calendar.getTime())+"=="+i); }
这种就能取出了
运行结果:
sdf=========2009-07-24==0
sdf=========2009-06-24==1
sdf=========2009-05-24==2
sdf=========2009-04-24==3
sdf=========2009-03-24==4
sdf=========2009-02-24==5
sdf=========2009-01-24==6
sdf=========2008-12-24==7
sdf=========2008-11-24==8
sdf=========2008-10-24==9
sdf=========2008-09-24==10
sdf=========2008-08-24==11