思路:
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
;