5700fca7 康伟

kangwei:

    1 设备运行时,补全日期、月份数据
1 个父辈 f79698db
package com.skua.modules.common.service;
import java.util.List;
import java.util.Map;
/**
* @auther kangwei
* @create 2024-11-19-12:09
*/
public interface IPgCommonService {
List<Map<String, Object>> queryForList(String sql);
Map<String, Object> queryForMap(String sql);
}
package com.skua.modules.common.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.skua.modules.common.service.IPgCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* @auther kangwei
* @create 2024-11-19-12:10
*/
@Service
@DS("pg-db")
public class PgCommonServiceImpl implements IPgCommonService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<Map<String, Object>> queryForList(String sql) {
return jdbcTemplate.queryForList(sql);
}
@Override
public Map<String, Object> queryForMap(String sql) {
return jdbcTemplate.queryForMap(sql);
}
}
......@@ -2,11 +2,9 @@ package com.skua.modules.guest.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Stream;
......@@ -97,6 +95,62 @@ public class DateUtil {
}
/***
* 获取两个时间的日期
* @param start
* @param end
* @return
*/
public static List<String> getDaysBetween(String start, String end,String format) {
List<String> days = new ArrayList<>();
if(format == null ){
format = "yyyy-MM-dd";
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
// 解析日期
LocalDate startDate = LocalDate.parse(start, formatter);
LocalDate endDate = LocalDate.parse(end, formatter);
LocalDate currentDate = LocalDate.now();//创建一个LocalData对象获取当前日期
if(endDate.isAfter( currentDate)){
endDate = currentDate;
}
long numOfDaysBetween = ChronoUnit.DAYS.between(startDate, endDate);
for (int i = 0; i <= numOfDaysBetween; i++) {
LocalDate date = startDate.plusDays(i);
days.add(date.toString()); // 格式化为 yyyy-MM
}
return days;
}
/***
* 获取两个时间的月份
* @param start
* @param end
* @return
*/
public static List<String> getMonthsBetween(String start, String end,String format) {
if(format == null ){
format = "yyyy-MM-dd";
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
// 解析日期
LocalDate startDate = LocalDate.parse(start, formatter);
LocalDate endDate = LocalDate.parse(end, formatter);
LocalDate currentDate = LocalDate.now();//创建一个LocalData对象获取当前日期
if(endDate.isAfter( currentDate)){
endDate = currentDate;
}
List<String> months = new ArrayList<>();
// 获取起始和结束月份
YearMonth startMonth = YearMonth.from(startDate);
YearMonth endMonth = YearMonth.from(endDate);
// 遍历月份并添加到列表
YearMonth currentMonth = startMonth;
while (!currentMonth.isAfter(endMonth)) {
months.add(currentMonth.toString()); // 格式化为 yyyy-MM
currentMonth = currentMonth.plusMonths(1);
}
return months;
}
/**
* 获取两个日期之间的所有日期 (年月日)
......@@ -123,10 +177,9 @@ public class DateUtil {
if(currentDate.getTime() < endDate.getTime() ){
endDate = currentDate;
}
//用Calendar 进行日期比较判断
Calendar calendar = Calendar.getInstance();
while (startDate.getTime() < endDate.getTime()){
while (startDate.getTime() <= endDate.getTime()){
// 把日期添加到集合
list.add(formatSDF.format(startDate));
// 设置日期
......
......@@ -121,4 +121,15 @@ public class MaterialInfo {
@Excel(name = "修订过程", width = 15)
@ApiModelProperty(value = "修订过程")
private String updateHistory;
@ApiModelProperty(value = "文件号")
private String fileCode;
@ApiModelProperty(value = "使用范围")
private String useScope;
@ApiModelProperty(value = "审核状态:0 未发布 1 已发布(待审核)2 审核驳回 3 审核通过")
@Dict(dicCode = "purchase_plan_status")
private String status;
}
......
......@@ -322,4 +322,14 @@ public class ScreenDataServiceImpl implements IScreenDataService {
return months;
}
public static void main(String[] args) {
String startTime = "2024-01-01";//
String endTime = "2024-12-31";
String format ="yyyy-MM-dd";
List<String> dateList = getMonthsBetween(startTime, endTime);
for(String date : dateList){
System.out.println("date:"+date);
}
}
}
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!