java 국가 법정 공휴일 과 휴일 판단 도구 류

4681 단어 자바
package com.date.dateutil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
* @author  qyw
* @description         
* @date Created in 21:01 2019/1/31
**/
public class ChineseCalendarUtils {

    //          
    //      。         ,       12      ,      
    // todo db   list,          
    private List lawHolidays   = Arrays.asList("2017-12-30", "2017-12-31",
            "2018-01-01", "2018-02-15", "2018-02-16", "2018-02-17", "2018-02-18",
            "2018-02-19", "2018-02-20", "2018-02-21", "2018-04-05", "2018-04-06",
            "2018-04-07", "2018-04-29", "2018-04-30", "2018-05-01", "2018-06-16",
            "2018-06-17", "2018-06-18", "2018-09-22", "2018-09-23", "2018-09-24",
            "2018-10-01", "2018-10-02", "2018-10-03", "2018-10-04", "2018-10-05",
            "2018-10-06", "2018-10-07");
    //              
    // todo db   list,      
    private List extraWorkdays = Arrays.asList("2018-02-11", "2018-02-24",
            "2018-04-08", "2018-04-28", "2018-09-29", "2018-09-30");

    /**
    * @author  qyw
    * @description           
    * @date Created in 21:03 2019/1/31
    **/
    public boolean isLawHoliday(String calendar) throws Exception {
        ChineseCalendarUtils.isValidDate(calendar);
        if (lawHolidays.contains(calendar)) {
            return true;
        }
        return false;
    }

    /**
    * @author  qyw
    * @description         
    * @date Created in 21:03 2019/1/31
    **/
    public boolean isWeekends(String calendar) throws Exception {
        ChineseCalendarUtils.isValidDate(calendar);
        //              Calendar  
        SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");
        Date             date = sdf.parse(calendar);
        Calendar         ca   = Calendar.getInstance();
        ca.setTime(date);
        if (ca.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
                || ca.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            return true;
        }
        return false;
    }

    /**
    * @author  qyw
    * @description                
    * @date Created in 21:06 2019/1/31
    **/
    public boolean isExtraWorkday(String calendar) throws Exception {
        ChineseCalendarUtils.isValidDate(calendar);
        if (extraWorkdays.contains(calendar)) {
            return true;
        }
        return false;
    }

    /**
    * @author  qyw
    * @description          (                )
    * @date Created in 21:06 2019/1/31
    **/
    public boolean isHoliday(String calendar) throws Exception {
        ChineseCalendarUtils.isValidDate(calendar);
        //              
        if (this.isLawHoliday(calendar)) {
            return true;
        }
        //                   
        if (!this.isWeekends(calendar)) {
            return false;
        }
        //                 
        if (this.isExtraWorkday(calendar)) {
            return false;
        }
        return true;
    }

    /**
    * @author  qyw
    * @description                 ,       ,2007/02/30  false
    * @date Created in 21:06 2019/1/31
    **/
    private static boolean isValidDate(String str) {
        boolean convertSuccess = true;
        //           /    /    ,  yyyy-MM-dd     ;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //   lenient false.
            //   SimpleDateFormat          ,  2007/02/29    ,    2007/03/01
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }


    public static void main(String[] args) throws Exception {
        String               calendar = "2018-01-32";
        ChineseCalendarUtils cc       = new ChineseCalendarUtils();
        System.out.println("   calendar          :"+ChineseCalendarUtils.isValidDate(calendar));
        System.out.println("        :" + cc.isLawHoliday(calendar));
        System.out.println("     :" + cc.isWeekends(calendar));
        System.out.println("            :" + cc.isExtraWorkday(calendar));
        System.out.println("      :" + cc.isHoliday(calendar));
    }
}

좋은 웹페이지 즐겨찾기