收藏 分销(赏)

JAVA-节假日-判断及配置.docx

上传人:仙人****88 文档编号:7185909 上传时间:2024-12-27 格式:DOCX 页数:6 大小:52.80KB 下载积分:10 金币
下载 相关 举报
JAVA-节假日-判断及配置.docx_第1页
第1页 / 共6页
JAVA-节假日-判断及配置.docx_第2页
第2页 / 共6页


点击查看更多>>
资源描述
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; import 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; **********日期加一天*********** 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()); 说明:以上代码来源于实际项目。            mdefSetting.getDate());    为 Date型            format.format(calendar.getTime()) 便是加 一天后的 时间,,,也为Date型 **********日期加一天*********** public class Festival { private final String FILE_NAME = "节假日.xls"; private List<Date> festival = new ArrayList<Date>();//节假日 private List<Date> workDay = new ArrayList<Date>();//工作日 public Festival(){    File excel = this.getExcel();       try {         FileInputStream fin = new FileInputStream(excel);     HSSFWorkbook hssfworkbook = new HSSFWorkbook(fin);     HSSFSheet sheet = hssfworkbook.getSheetAt(0);     int last = 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 = HSSFDateUtil.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)){        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 (FileNotFoundException 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 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.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();    dcal.setTime(date);    List<Date> list = this.getFestival();    for(Date dt:list){     fcal.setTime(dt);         //法定节假日判断     if(fcal.get(Calendar.MONTH)==dcal.get(Calendar.MONTH)&&       fcal.get(Calendar.DATE)==dcal.get(Calendar.DATE))     {      festival = true;     }    }    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)==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;     }         /*特殊工作日判断*/     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)&&        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(str);    } 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)); } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服