Workday Computing Tool

Before writing some business logic, there are some logic to calculate the workday. It's different every time. Recently, I took time to write an interface and hang it on the server. For brothers in need.

 

1. Method Description:

1,  http://129.211.22.18:8080/Statistics/afterWorkDay/2019-9-5/3

The address above is the afterWorkDay interface that is called to calculate the dates of several working days in the future. The example means calculating the date of the third working day after September 5, 2019. The last two parameters can be filled in according to the actual situation.

 

2,http://129.211.22.18:8080/Statistics/beforeWorkDay/2019-9-5/3

This method is the same as the first method passing parameters. But it calculates the number of working days before a certain day. The example means calculating the date of September 5, 2019 and pushing forward three working days.

 

3,http://129.211.22.18:8080/Statistics/calcWorkDay/2019-8-13/2019-9-5

This method calculates how many working days are separated between two dates. It should be noted that the first date parameter needs to be smaller than the second date parameter or return 0.

 

2. Use method:

The server returns an object as follows: {"id": 20842, "workdate", "2019-08-28", "week_day": 3, "isHoliday": 0}

IDS need not be ignored. workdate is the target date. Wednesday is Wednesday, and Wednesday is Wednesday. Is it Holiday a holiday? 0 is not a holiday.

 

If you use java. Here's an encapsulated class. Then refer to the direct call in the main method.

 

The code is as follows:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

public class WebServiceTest {
	
	public static String afterWorkDay(String startDate , int distance) {
		return sendGet("afterWorkDay/"+startDate+"/"+distance);
	}
	
	public static String beforeWorkDay(String startDate , int distance) {
		return sendGet("beforeWorkDay/"+startDate+"/"+distance);
	}
	
	public static String calcWorkDay(String startDate , String endDate) {
		return sendGet("calcWorkDay/"+startDate+"/"+endDate);
	}
    
	
	/**
     * Send a GET method request to the specified URL
     * 
     * @param url
     *            The URL to send the request
     * @param param
     *            The request parameter should be in the form of name1 = value1 & Name2 = value2.
     * @return URL Response results of the remote resources represented
     */
    public static String sendGet(String param) {
    	String url = "http://129.211.22.18:8080/Statistics";
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "/" + param;
            URL realUrl = new URL(urlNameString);
            // Open the connection between the URL
            URLConnection connection = realUrl.openConnection();
            // Setting Common Request Properties
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // Establish actual connections
            connection.connect();
            // Get all response header fields
            //Map<String, List<String>> map = connection.getHeaderFields();
            // Traverse through all response header fields
            //for (String key : map.keySet()) {
            //   System.out.println(key + "--->" + map.get(key));
            //}
            // Define the BufferedReader input stream to read the response of the URL
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("Send out GET The request is abnormal!" + e);
            e.printStackTrace();
        }
        // Close the input stream using the final block
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
    
    public static void main(String[] args) {
		//Objects returned by the output server
    	System.out.println(afterWorkDay("2019-8-24", 3));
		//This is to turn the return object into a json object, using Ali's fastjson package
    	JSONObject data = JSONObject.parseObject(afterWorkDay("2019-8-24", 3));
		//When converted into a json object, the corresponding attributes can be obtained.
    	System.out.println(data.get("workdate").toString());
		System.out.println(data.get("week_day").toString());
		System.out.println(data.get("isHoliday").toString());
	}

}

Welcome comments

Tags: Java JSON Windows

Posted on Wed, 04 Sep 2019 19:38:03 -0700 by jrmckee