java获取香港法定节假日
时间: 2023-08-08 22:10:07 浏览: 109
要获取香港法定节假日,需要使用香港政府的公开API。具体步骤如下:
1. 在网站 https://data.gov.hk/en-data/dataset/hk-holidays-and-notable-dates/resource/316e1e9e-8d9a-4e9c-b6a4-8f1c163d3a69 上获取API Key。
2. 使用Java中的HttpURLConnection或HttpClient等类库,向API地址发送请求并获取返回结果。
3. 解析返回结果,获取需要的节假日信息。
以下是一个简单的Java代码示例,演示如何使用HttpURLConnection获取香港法定节假日:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HKHolidayAPI {
public static void main(String[] args) throws Exception {
String url = "https://api.data.gov.hk/v1/holidays/general";
String apiKey = "<your-api-key>";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 设置请求头
con.setRequestMethod("GET");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Authorization", "Bearer " + apiKey);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 解析返回结果
System.out.println(response.toString());
}
}
```
上述代码中,需要把`<your-api-key>`替换为你自己的API Key。运行代码后,会在控制台输出获取到的JSON格式的节假日信息。你可以根据需要解析JSON数据,提取出需要的香港法定节假日信息。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)