`
diarbao
  • 浏览: 14276 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JAVA_自动获取一年内所有星期六的日期

阅读更多
public class PrintDate {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Calendar calendar=Calendar.getInstance();//当前日期
  int year=calendar.get(Calendar.YEAR);//2010
  int nextyear=calendar.get(Calendar.YEAR)+1;//2011;
  Calendar nowyear=Calendar.getInstance();
  Calendar nexty=Calendar.getInstance();
  nowyear.set(year, 0, 1);//2010-1-1
  nexty.set(nextyear, 0, 1);//2011-1-1
  
  calendar.add(Calendar.DAY_OF_MONTH, -calendar.get(Calendar.DAY_OF_WEEK));//周六
  Calendar c=(Calendar) calendar.clone();
  for(;calendar.before(nexty)&&calendar.after(nowyear);calendar.add(Calendar.DAY_OF_YEAR, -7)){
   printf(calendar);
  }
  for(;c.before(nexty)&&c.after(nowyear);c.add(Calendar.DAY_OF_YEAR, 7)){
   printf(c);
  }
     }
     
     //打印
     public static void printf(Calendar calendar)
     {
         System.out.println(calendar.get(Calendar.YEAR)+"-"+(1+calendar.get(Calendar.MONTH))+"-"+calendar.get(Calendar.DATE));
         
     }
 }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics