判断日期是否为周末 java_Java 判断日期是否是节假日、是否是周末,当月所有节假日...

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.Locale;/**

* Created by weinee on 16/3/7.*/

public classAPIHelper {private static final String API_KEY = "db6dd4d854a912463898aad522032288";/** guess游戏获取上证指数的API*/

private static final String GAME_GUESS_API = "http://apis.baidu.com/apistore/stockservice/stock";/** 判断某天是否为节假日的API*/

private static final String GETHOLIDA_API = "http://apis.baidu.com/xiaogg/holiday/holiday";/** 进行API请求的最大次数*/

private static final int COUNT = 5;/**

* 判断所给日期为什么类型,

* @param date 日期

* @return 0 工作日, 1 休息日, 2 节假日, -1 为判断出错*/

public static intholidayType(Date date){

String dateStr= "";

Integer result= -1;

DateFormat sdf= new SimpleDateFormat("yyyyMMdd");try{

dateStr=sdf.format(date);

}catch(Exception e) {

e.printStackTrace();

}

String httpArg= "d=" +dateStr;

String jsonResult=request(GETHOLIDA_API, httpArg);if(StringUtils.isNumeric(jsonResult)){try{

result=Integer.parseInt(jsonResult);

}catch(Exception e){

e.printStackTrace();

}

}else{

result=isWeeked(date);

}returnresult;

}public staticJSONObject getStockinfo(){

JSONObject result= null;

String httpArg= "stockid=sh002230&list=1";for (int i=0; i

String jsonResult=request(GAME_GUESS_API, httpArg);

JSONObject jsonObject=JSONObject.fromObject(jsonResult);if(jsonObject.getInt("errNum")==0) {

result= jsonObject.getJSONObject("retData").getJSONObject("market").getJSONObject("shanghai");break;

}

}if (result != null){//判断是否为节假日

if (holidayType(new Date()) !=0){

result= null;

}

}returnresult;

}/**

* @param httpUrl

* :请求接口

* @param httpArg

* :参数

* @return 返回结果*/

public staticString request(String httpUrl, String httpArg) {

BufferedReader reader= null;

String result= null;

StringBuffer sbf= newStringBuffer();

httpUrl= httpUrl + "?" +httpArg;try{

URL url= newURL(httpUrl);

HttpURLConnection connection=(HttpURLConnection) url

.openConnection();

connection.setRequestMethod("GET");//填入apikey到HTTP header

connection.setRequestProperty("apikey", API_KEY);

connection.connect();

InputStreamis =connection.getInputStream();

reader= new BufferedReader(new InputStreamReader(is, "UTF-8"));

String strRead= null;while ((strRead = reader.readLine()) != null) {

sbf.append(strRead);

}

reader.close();

result=sbf.toString();

}catch(Exception e) {

e.printStackTrace();

}returnresult;

}/**

* 计算是否为周末 周日或周六

* @param date 传入的时间

* @return -1有错误; 0不是周末; 1是周末*/

public static intisWeeked(Date date){int result = 0;try{

Calendar cal=Calendar.getInstance();

cal.setTime(date);int week_index = cal.get(Calendar.DAY_OF_WEEK);if(week_index==1 || week_index == 7){

result= 1;

}

}catch(Exception e){

result= -1;

e.printStackTrace();

}returnresult;

}/**

* 获取当月所有节假日

* @param date 日期

* @return*/

public staticString getHolidayOfMonth(){

String dateStr= "";

Calendar calendar=Calendar.getInstance();

calendar.setTime(newDate());

calendar.add(Calendar.MONTH,1);int days =calendar.getActualMaximum(Calendar.DATE);int year = calendar.get(Calendar.YEAR);int month = calendar.get(Calendar.MONTH);if(month < 12){

month+= 1;

}if(month == 12){

month= 1;

year+= 1;

}for(int i = 1; i <= days; i++){

String day= i + "";if(i < 10){

day= "0"+i;

}

String mon= month + "";if(month < 10){

mon= "0" +month;

}

dateStr+= year + mon +day;if(i

dateStr+= ",";

}

}

String httpArg= "d=" +dateStr;

String jsonResult=request(GETHOLIDA_API, httpArg);returnjsonResult;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
判断当月日期是否节假日,可以使用中国的法定节假日时间表,根据日期判断是否节假日范围内。以下是JAVA判断当月日期是否节假日的代码示例: ```java import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Holiday { /** * 判断当月日期是否节假日 * @param date 当前日期 * @return 是否节假日 */ public static boolean isHoliday(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH); // 元旦节 if (month == 1 && day == 1) { return true; } // 春节 if (month == 2 && (day == 4 || day == 5 || day == 6 || day == 7 || day == 8 || day == 9 || day == 10)) { return true; } // 清明节 if (month == 4 && (day == 5 || day == 6 || day == 7)) { return true; } // 劳动节 if (month == 5 && (day == 1 || day == 2 || day == 3)) { return true; } // 端午节 if (month == 6 && (day == 12 || day == 13 || day == 14)) { return true; } // 中秋节 if (month == 9 && (day == 19 || day == 20 || day == 21)) { return true; } // 国庆节 if (month == 10 && (day == 1 || day == 2 || day == 3 || day == 4 || day == 5 || day == 6 || day == 7)) { return true; } return false; } public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = new Date(); boolean isHoliday = isHoliday(date); System.out.println(sdf.format(date) + " 是否节假日:" + isHoliday); } } ``` 在这个代码中,我们定义了一个 `isHoliday` 方法,用于判断当月日期是否节假日。在 `main` 方法中,我们获取当前日期,并判断是否节假日。如果是节假日,则返回 `true`,否则返回 `false`。在代码中,我们使用了 `java.util.Calendar` 类来获取年月日等信息,使用了 `java.text.SimpleDateFormat` 类来格式化日期
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值