获取全年日期,节假日

思路:
1、获取全年日期
2、调用接口获取全年的节假日
3、然后获取全年的周六和周末

 public static void main(String[] args) throws Exception {

    Map mapTypes = new HashMap<Object, Object>();
    String date = "";

    //获取一年所有日期
    LocalDate startDate = LocalDate.of(2024, 1, 1);
    LocalDate endDate = LocalDate.of(2024, 12, 31);
    Period period = Period.between(startDate, endDate);
    LocalDate currentDate = startDate;
    String year = String.valueOf(currentDate.getYear());


    // 获取节假日不包含双休
    String url = "https://timor.tech/api/holiday/year/" + year;
    try {
        String s1 = HttpClientUtils.get(url, "");
        


  		     JSONObject jsonObject = JSONObject.parseObject(s1);
       	 String holiday = jsonObject.getString("holiday");

        mapTypes = JSON.parseObject(holiday);

    } catch (Exception e) {
        e.printStackTrace();
    }

    while (currentDate.isBefore(endDate) || currentDate.isEqual(endDate)) {

        String monthValue = String.valueOf(currentDate.getMonthValue());
        if (monthValue.length() < 2) {
            monthValue = "0" + monthValue;
        }
        String dayOfMonth = String.valueOf(currentDate.getDayOfMonth());
        if (dayOfMonth.length() < 2) {
            dayOfMonth = "0" + dayOfMonth;
        }
        date = year + "-" + monthValue + "-" + dayOfMonth;
        JSONObject jsonObject1 = new JSONObject();
        String name = "";
        String date_type = "节假日";
        int wage = 0;
        try {
            if (mapTypes.containsKey(monthValue + "-" + dayOfMonth)) {
                String s = mapTypes.get(monthValue + "-" + dayOfMonth).toString();
                jsonObject1 = JSONObject.parseObject(mapTypes.get(monthValue + "-" + dayOfMonth).toString());
                System.out.println(jsonObject1.toString());
                // 节日注释
                name = jsonObject1.getString("name");
                //1 调班 2,3 节假日,
                wage = jsonObject1.getIntValue("wage");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (wage == 1) {
            date_type = "工作日";
        } else if (wage == 0) {
            int i = dayForWeek(date);
            if (i == 7 || i == 6) {
                date_type = "节假日";
            }else {
                date_type = "工作日";
            }
        }

        String sql = String.format("INSERT  into test009.ods_bsn_holiday(date,date_type,remarks) values ('%s','%s','%s')", date, date_type, name);
        specificDate.testConnection(sql);
        currentDate = currentDate.plusDays(1);
    }


}

  public static int dayForWeek(String pTime) throws Exception {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.setTime(format.parse(pTime));
    int dayForWeek = 0;
    if (c.get(Calendar.DAY_OF_WEEK) == 1) {
        dayForWeek = 7;
    } else {
        dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
    }
    return dayForWeek;
}

public static void testConnection(String sql) throws Exception {
    String url = "jdbc:mysql://IP:9030/test009";
    String user = "NAME";
    String password = "PASSWORD";

    //2. 加载Driver
    Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
    Driver driver = (Driver) clazz.newInstance();
    DriverManager.registerDriver(driver);

    //获取连接
    try {
        Connection conn = DriverManager.getConnection(url, user, password);
        Statement statement = conn.createStatement();
        int i = statement.executeUpdate(sql);
        System.out.println("操作条数为: " + i);
    } catch (SQLException e) {
        e.printStackTrace();
    }

}

排序,周六周末,节假日

   SELECT
        date
        ,date_type
        ,remarks
-- 加前一年最后一天的排序号
,SUM(local_sort) OVER (ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) + 748 as sort
,now() as create_time
,now() as update_time
,'aaa' as create_user
,'aaaa' as update_user
,WEEKDAY(date)+1 as  week_day
from
        (
                SELECT *
                ,if(date_type ="节假日" ,0,1) as local_sort
from test009.ods_bsn_holiday
where date >='2024-01-01'
        -- DATE_FORMAT(NOW(),'%Y-01-01')
order by date
) aa
order by date
;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值