1、 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import .MalformedURLException; import .URL; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; im
2、port java.util.Date; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; ******
3、日期加一天*********** DateFormat format=new SimpleDateFormat("yyyy-MM-dd"); Calendar calendar=Calendar.getInstance(); calendar.setTime(mdefSetting.getDate()); calendar.add(Calendar.DAY_OF_MONTH,1); mdefs.setName(format.format(calendar.getTime())+" 至 "+mdefSetting2.getDate()); 说明
4、以上代码来源于实际项目。
mdefSetting.getDate()); 为 Date型
format.format(calendar.getTime()) 便是加 一天后的 时间,,,也为Date型
**********日期加一天***********
public class Festival {
private final String FILE_NAME = "节假日.xls";
private List
5、 List
6、 = sheet.getLastRowNum(); int index = 1; Date dt = null; while(index<=last){ HSSFRow row = sheet.getRow(index); /*读取法定节假日*/ HSSFCell cell = row.getCell((short)0); if(cell!=null){ if(HSSFDateUtil.isCellDateFormatted(cell)){ dt = HSSFDat
7、eUtil.getJavaDate(cell.getNumericCellValue()); if(dt!=null&&dt.getTime()>0){ this.festival.add(dt); } } } /*读取特殊工作日*/ cell = row.getCell((short)1); if(cell!=null){ if(HSSFDateUtil.isCellDateFormatted(cell)){
8、 dt = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()); if(dt!=null&&dt.getTime()>0){ //System.out.println(this.getDate(dt)); this.workDay.add(dt); } } } index++; } fin.close(); } catch (FileNotFoundExcepti
9、on e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public File getExcel(){ File excel = null; try { URL url = Festival.class.getResource("/"); url = new
10、URL(url,"../"+FILE_NAME); excel = new File(url.getPath()); return excel; } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return excel; } /** * 从EXCEL文件中读取节假日 * @return */ public List getFestival(){ return this.
11、festival; } public List getSpecialWorkDay(){ return this.workDay; } /** * 判断一个日期是否日节假日 * 法定节假日只判断月份和天,不判断年 * @param date * @return */ public boolean isFestival(Date date){ boolean festival = false; Calendar fcal = Calendar.getInstance(); Calendar dcal = Calendar.getInstance
12、);
dcal.setTime(date);
List
13、 return festival; } /** * 周六周日判断 * @param date * @return */ public boolean isWeekend(Date date){ boolean weekend = false; Calendar cal = Calendar.getInstance(); cal.setTime(date); if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY|| cal.get(Calendar.DAY_OF_WEEK)==
14、Calendar.SUNDAY){ weekend = true; } return weekend; } /** * 是否是工作日 * 法定节假日和周末为非工作日 * @param date * @return */ public boolean isWorkDay(Date date){ boolean workday = true; if(this.isFestival(date)||this.isWeekend(date)){ workday = false; } /*特殊工作
15、日判断*/ Calendar cal1 = Calendar.getInstance(); cal1.setTime(date); Calendar cal2 = Calendar.getInstance(); for(Date dt:this.workDay){ cal2.setTime(dt); if(cal1.get(Calendar.YEAR)==cal2.get(Calendar.YEAR)&& cal1.get(Calendar.MONTH)==cal2.get(Calendar.MONTH)&&
16、 cal1.get(Calendar.DATE)==cal2.get(Calendar.DATE) ){ //年月日相等为特殊工作日 workday = true; } } return workday; } public Date getDate(String str){ Date dt = null; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); try { dt = df.parse(s
17、tr); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return dt; } public String getDate(Date date){ String dt = null; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); dt = df.format(date); return dt; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Festival f = new Festival(); Date dt = f.getDate("2009-09-26"); System.out.println(f.isWorkDay(dt)); } }






