java 传入参数(天数)返回工作日期(不包括周六日)

package com.dking.test;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


public class TestDate {
public static void main(String[] args) {
getWorkDay(4);
}
  public static List getWorkDay(int past){
  //获取当天日期
  List listHolidays = new ArrayList();
  Calendar sdate = Calendar.getInstance();
  int count = 0;
  /*if(sdate.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY){
  count = 5;
  }else if(sdate.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY){
  count = 6;
  }else if(sdate.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY){
  count = 6;
  }else if(sdate.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY){
  count = 6;
  }else if(sdate.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY){
  count = 6;
  }else if(sdate.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){
  count = 6;
  }else if(sdate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
  count = 5;
  }*/
  if(sdate.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY || sdate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
  count = 5;
  }else{
  count = 6;
  }
  //判断当天是否是周末
  if(sdate.get(Calendar.DAY_OF_WEEK) !=Calendar.SUNDAY && sdate.get(Calendar.DAY_OF_WEEK) !=Calendar.SATURDAY){
  listHolidays.add(sdate.getTime());
  }
  System.out.println("count:"+count);
  //循环次数
  for(int i=0;i   sdate.set(Calendar.DAY_OF_YEAR, sdate.get(Calendar.DAY_OF_YEAR)+1);
  if(sdate.get(Calendar.DAY_OF_WEEK) !=Calendar.SUNDAY && sdate.get(Calendar.DAY_OF_WEEK) !=Calendar.SATURDAY){
  listHolidays.add(sdate.getTime());
  } 
  //第一种方式传参数返回
  if(listHolidays.size() == past){
  break;
  }
  }
  //迭代转换格式 
  Iterator iterator = listHolidays.iterator();
  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
  List dateStr = new ArrayList();
  int j = 0;
  while (iterator.hasNext()) {
  Date hdate = iterator.next();
  String result = format.format(hdate);  
  //第二种方式传参数返回
  /*if(j++ == past){
  break;
  }else{
  dateStr.add(result);
  }*/
  dateStr.add(result);
  System.out.println(result);
  }
  return dateStr;
  }


}

你可能感兴趣的:(java 传入参数(天数)返回工作日期(不包括周六日))