Java 当前日期判断节假日

Java关于日期处理

  1. 获取今天日期(today)

    	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	Date date = new Date();
    	String today = dateFormat.format(date);
    
  2. 获取明天日期(tomorrow)

    	Date today = new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String yesterday = simpleDateFormat.format(today);//获取明天日期
    
  3. 获取昨天日期(yesterday)

    	Date today = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String yesterday = simpleDateFormat.format(today);//获取昨天日期
    
  4. 判断是否是工作日

    public static String request(String httpArg) {
        String httpUrl = "http://tool.bitefu.net/jiari/";
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?d=" + httpArg;
        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);
    
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
    
        }
    
        return result;
    
    }
    
    public String dateType(String httpArg){
        String jsonResult = request(httpArg);
        //String jsonResult = HolidayUtil.request("20220715");
        // 0 上班 1周末 2节假日
        String type = null;
       switch (jsonResult){
           case "0":
               type = "工作日";
               break;
           case "1":
               type = "周末";
               break;
           case "2" :
               type = "节假日";
               break;
    
       }
       return type;
    }
    
  5. 判断是否是节假日

    public static String request(String httpArg) {
        String httpUrl = "http://tool.bitefu.net/jiari/";
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?d=" + httpArg;
        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);
    
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
    
        }
    
        return result;
    
    }
    
    public String dateType(String httpArg){
        String jsonResult = request(httpArg);
        //String jsonResult = HolidayUtil.request("20220715");
        // 0 上班 1周末 2节假日
        String type = null;
       switch (jsonResult){
           case "0":
               type = "工作日";
               break;
           case "1":
               type = "周末";
               break;
           case "2" :
               type = "节假日";
               break;
    
       }
       return type;
    }
    
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java判断当前日期是否节假日和周六周日可以使用Java日期类库和国家法定节假日规定来判断。 首先,获取当前日期可以使用Java的Date类或Calendar类。例如: ```java Date date = new Date(); //获取当前日期 Calendar calendar = Calendar.getInstance(); //获取当前日期 ``` 判断当前日期是否是周六或周日,可以使用Calendar类的get方法获取当前日期的星期几,如果是1(星期日)或7(星期六)则为周末。例如: ```java int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); //获取当前星期几 if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) { System.out.println("今天是周末"); } ``` 判断当前日期是否是节假日,可以使用国家法定节假日规定来判断。例如,在中国,国家法定节假日包括春节、清明节、劳动节、端午节、中秋节、国庆节等。以下是一个简单的判断方法: ```java SimpleDateFormat sdf = new SimpleDateFormat("MMdd"); String dateStr = sdf.format(date); if ("0101".equals(dateStr)) { System.out.println("今天是元旦节"); } else if ("0501".equals(dateStr)) { System.out.println("今天是劳动节"); } else if ("1001".equals(dateStr)) { System.out.println("今天是国庆节"); } else { System.out.println("今天不是节假日"); } ``` 需要注意的是,国家法定节假日可能会根据政策改变,因此以上判断方法可能不完全准确。如果需要更精确的判断,可以使用一些专门的日期计算工具或API。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值