According weekday / holiday Receipt required to calculate estimated delivery time

1. Demand: The next single orderTime (this is the default system time); customer receipt time requirements: days = 1, 2 holidays; calculating estimated delivery time (here need to transport long);

2. The idea: To calculate the estimated time of delivery, (warehouse delivery deadline is the day 16:30; 16:30 later as the next day's orders )

    First, calculate the normal arrival time, for example, under normal circumstances: the order is a list of the August 16, Shanghai -> Nanjing, Jiangsu Province  , August 16 16:30 shipments, transport time of 17 hours will be in stock time is August 17 at 9:30 (Saturday) ,

    Then, based on the expected arrival time begin to calculate customer requirements weekday / holiday time if the customer holiday shipping instructions number at 16:30 on August 16 to normal delivery, delay time. DelayDays = 0;

    If customers need business days of delivery (ie, delivery time is August 19 at 9:30 (Monday)   ), delayed delivery several days delayDays = 2, that is, August 18 16:30 delivery.

3. Difficulties and solutions: How difficult is the place to get the expected arrival time of a weekday / holiday, and found to meet the requirements of the expected arrival time.

      Basic solutions are two,

  First, maintain a calendar , designed to query days are working or holidays advantage is relatively easy to obtain data, easy access; Cons: holidays make up classes and are difficult to maintain, may not have readily available data on hand.

  Second, ask a third party website for holiday types, advantages: easier to find, acquire direct HTTP request; Disadvantages: third-party websites stability and security issues , may cause can not be used, can not be queried;

4 I am here to choose the second option, of course, be allowed to consult the leadership of the use of third-party websites http://api.goseek.cn; if the third party in the case of instability, direct judged according to the normal weekday and weekend dates can;

The acquisition code is provided below the number of days of delivery delay, (warehouse delivery deadline day 16:30 , where the default delivery time is 16:30, the specific time may be self-adjusting express delivery)

 

6. Request third-party website tools

Package utils.time; 

Import net.sf.json.JSONObject; 

Import java.io.BufferedReader;
 Import a java.io.InputStream;
 Import the java.io.InputStreamReader;
 Import java.net.HttpURLConnection The;
 Import the java.net.URL;
 Import the java.text.SimpleDateFormat; 


public  class HolidayUtil {
     / * 
    Get the specified date holiday information 
    1, interface address: http://api.goseek.cn/Tools/holiday?date= digital date support the https protocol. 
    2, returns the data: the result is 0 corresponds to a normal working day, holidays correspond result is 1, the result of holidays workdays corresponding to 2 days off the corresponding result of 3 
    3 holiday Data Description: This interface contains since 2017 Chinese legal holidays of data, data released by the State Department sources announcement, updated once a year to ensure that the latest data 
    4, example: request and return data format
    http://api.goseek.cn/Tools/holiday?date=20191005     { "code": 10000, "Data": National Day}. 1
     http://api.goseek.cn/Tools/holiday?date=20190805        { "code": 10000, "data ": 0} Monday
     http://api.goseek.cn/Tools/holiday?date=20190817        { "code": 10000, "Data": Saturday. 3}
     http: // api ? .goseek.cn / Tools / Holiday 20,190,818 DATE =       { "code": 10000, "Data": Sunday. 3}
     http://api.goseek.cn/Tools/holiday?date=20190815      { "code": 10000, "data": 0} Thursday
     http://api.goseek.cn/Tools/holiday?date=20190913     { "code": 10000, "Data": Mid. 1}
     http: //api.goseek.cn/Tools/holiday?date=20190929    {"code":10000,"data":2}     调休补班
    * */

    public static Integer request(String httpArg) {
        String httpUrl =  "http://api.goseek.cn/Tools/holiday?date=";
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl += httpArg;

        // int d = 0;
        Integer d = null;  // 节假日类型,这里默认为null

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            Result = sbf.toString (); 
            the JSONObject OB = JSONObject.fromObject (Result);
             IF (! OB = null ) { 
                D = the Integer.parseInt (ob.getString ( "Data" )); 
            } 

        } the catch (Exception E) { 
            e.printStackTrace (); 
        } 
        return D; 
    } 


    / * requests example * / 
    public  static  void main (String [] args) {
         // determines whether today is the day on weekends or holidays, 
        the SimpleDateFormat F = new new the SimpleDateFormat ( "yyyyMMdd" );
        HttpArg String = "20,190,216"; // f.format (new new a Date ()); 
        System.out.println (httpArg);
         int n-= Request (httpArg); 
        System.out.println (httpArg + "weekday determination request holidays examples of results: "+ n-);
         // days corresponding to the result is 0, the corresponding result is a rest day, holiday result corresponding 2 

    } 

}

 

7. Calculate the number of days delay delivery

 

Package utils.time; 

Import the java.text.SimpleDateFormat;
 Import the java.util.Calendar;
 Import java.util.Date; 

public  class DelayDaysDemo { 

    public  static Integer SCHEDULLE_TYPE_WEEK = 1;   // pass delivery delay requirement,: = 1 workday 2 = holiday, 
    public  static Integer SCHEDULLE_TYPE_WEEKEND = 2;   // transmission delay delivery claim: 1 = weekday, holiday = 2, 

    public  static String TIME_FORM_HOLIDAY = "yyyyMMdd";   // determine whether the date is the day on weekends and holidays time format 

    / ** 
     * @Param: startDate, expected arrival date (* required)  
     * @Param: scheduleType transfer delay delivery requirements: 1 = weekdays, 2 = holidays, (* required)
     * @Description: get the first working day of rest or start date, number of days to return later postponed startDate 
     * @author: ZYF 2019/8/14 
     * / 
    public  static Integer getDelayDay (startDate a Date, Integer scheduleType) throws Exception {
         // priority send http request third-party sites selected dates, followed by more normal weekdays and weekends Saturday 
        SimpleDateFormat f = new new SimpleDateFormat (TIME_FORM_HOLIDAY);   // judged format of the date 
        / * from the date of the beginning of the day, to push back within 30 days time, date meet the requirements to find * / 
        for ( int ;; I <++ 30 I = I 0 {) 

            String formatDay = f.format (startDate); 

            Integer holidayType = null ;// default null, weekday / holiday type: 1 = weekdays, holidays = 2, corresponding to the type of latency requirements 1,2
            the try {
                 // call the third-party tools to determine whether today is the day on weekends or holidays, the type of data returned, weekday = 0, = 1 holidays, workdays = 2, 3 rest days = 
                Integer = responseType HolidayUtil.request (formatDay);
                 // int. 1 C = / 0;   // test to third abnormal 

                // matches the type of the returned working / holiday type 
                IF (responseType =! null ) {
                     Switch (responseType) {
                         Case 0 : 
                            holidayType = SCHEDULLE_TYPE_WEEK; // 1, 1 = weekday, holiday = 2 
                            BREAK ;
                         :Case . 1 
                            holidayType = SCHEDULLE_TYPE_WEEKEND; // 2, working day. 1 = 2 = Holiday 
                            BREAK ;
                         Case 2 : 
                            holidayType = SCHEDULLE_TYPE_WEEK; // . 1,. 1 = weekdays, holidays = 2 
                            BREAK ;
                         Case . 3 : 
                            holidayType = SCHEDULLE_TYPE_WEEKEND; // 2, 1 = weekdays, holidays = 2 
                            BREAK ;
                         default :
                             BREAK ; 
                    } 
                } 
            } the catch (Exception E) {
                 // logger.error (formatDay + "determines whether to call a third party tool working abnormal !!", E); 
                e.printStackTrace (); 
                System.out.println (formatDay + "call third-party tools is determined whether abnormal working !! " );
                 // abnormality continues down 
            } 

            System.out.println (formatDay +" weekday / holiday type is: "+ holidayType); 

            // the try to third the exception request, if abnormal, normal walking type judgment weekend week 
            IF (holidayType == null ) { 
                holidayType = dayTypeForWeek (startDate); 
            } 

            IF (== scheduleType holidayType) {
                 returnI;        // if the type of traversal latency requirements to comply with the current date type otherwise proceed directly back 
            } the else { 
                startDate = getNextMoreDay (startDate,. 1);    // type does not match, the number of days + 1'd 
                Continue ; 
            } 

        } 

        return  null ;   // do not find a direct return null 

    } 


    / ** 
     * @param PDATE repair time to be determined 
     * @return dayForWeek judgment result 
     * @Exception determine whether it is the weekend, // 1 = working day, holidays = 2 
     * / 
    public  static Integer dayTypeForWeek (a Date PDATE) throws = Exception {
        CAL Calendar Calendar.getInstance (); 
        cal.setTime (PDATE); 
        int Week = cal.get (the Calendar.DAY_OF_WEEK) -. 1 ;
         IF (Week Week == == 0 ||. 6) { // 0 representatives Sunday, 6 represents Saturday 
            return 2 ; 
        } 
        return . 1 ; 
    } 


    / ** 
     * @Param: sourceDate original date 
     * @Param: days after n n 
     * @Description: Get the next N days 
     * @Author: zyf 2019/8/14 
     * / 
    public  static a Date getNextMoreDay (a Date sourceDate, int n-) {
         int oneDayTime = 24 * 60 * 60 * 1000;        // represents a day of milliseconds 
        long= sourceDate.getTime initMilliSeconds () * + oneDayTime n-;
         return  new new Date (initMilliSeconds);   // ms formatted Date type 
    } 


}

 

8. originally estimated delivery time + n = number of days late delivery of real time delivery is expected.

 

9. summary

 ① choose the right solution to the problem. Road ten million ~

   ② specific issues requiring specific analysis, expected delivery time is only a rough calculation of the time here, transport limitations specific time was kind enough to express, express arrival (delivery) time-dependent.

For example, calculated by the program estimated time of arrival is on Friday at 20:00, this time it count days or holidays? Here we must continue to optimize the business according to the actual calculation of the ~

 

Guess you like

Origin www.cnblogs.com/coloz/p/11349662.html