更新StringUtils

This commit is contained in:
JIAL 2024-01-15 11:26:38 +08:00
parent aabb4267b9
commit b3d54bc25f
3 changed files with 32 additions and 15 deletions

View File

@ -77,9 +77,9 @@ public class BizController {
Map<String, String> resultMap = new HashMap<>();
resultMap.put("latestShippingDate", "0".equals(latestShippingDate) ? latestShippingDate : StringUtils.formatDateString(latestShippingDate, "yyyyMMdd", "yyyy-MM-dd"));
resultMap.put("latestOrderDate", "0".equals(latestOrderDate) ? latestOrderDate : StringUtils.formatDateString(latestOrderDate, "yyyyMMdd", "yyyy-MM-dd"));
resultMap.put("latestQuotationDate", "0".equals(latestQuotationDate) ? latestQuotationDate : StringUtils.formatDateString(latestQuotationDate, "yyyy-MM-dd HH:mm:ss.SS", "yyyy-MM-dd"));
resultMap.put("latestShippingDate", "0".equals(latestShippingDate) ? latestShippingDate : StringUtils.formatDateString(latestShippingDate, "yyyy-MM-dd"));
resultMap.put("latestOrderDate", "0".equals(latestOrderDate) ? latestOrderDate : StringUtils.formatDateString(latestOrderDate, "yyyy-MM-dd"));
resultMap.put("latestQuotationDate", "0".equals(latestQuotationDate) ? latestQuotationDate : StringUtils.formatDateString(latestQuotationDate, "yyyy-MM-dd"));
resultMap.put("salespersonCount", salespersonCount );
log.info("Received request with parameter: {}", "开始写日志文件");

View File

@ -1,7 +1,10 @@
package com.JIAL.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Optional;
/**
@ -29,20 +32,34 @@ public class StringUtils {
/**
* @title formatDateString
* @description 格式化传入的日期字符串
* @author JIAL
* @param: inputString 传入字符串
* @param: inputFormat 指定传入的格式 yyyyMMdd
* @param: outputFormat 指定输出的格式 yyyy-MM-dd
* @updateTime 2024/1/9 14:39
* @author JN
* @param: dateTimeString 日期字符串 支持yyyy-MM-dd HH:mm:ss,SSS(小数点位数不固定)支持yyyyMMdd
* @param: pattern 你想要输出的格式
* @updateTime 2024/1/15 9:52
* @return: java.lang.String
*/
public static String formatDateString(String inputString, String inputFormat, String outputFormat) {
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(inputFormat);
LocalDate date = LocalDate.parse(inputString, inputFormatter);
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(outputFormat);
return date.format(outputFormatter);
}
public static String formatDateString(String dateTimeString, String pattern) {
try {
// 将时间字符串解析为 Date 对象
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date parsedDate = inputFormat.parse(dateTimeString);
// 格式化 Date 对象为指定格式的字符串
SimpleDateFormat outputFormat = new SimpleDateFormat(pattern);
return outputFormat.format(parsedDate);
} catch (ParseException e) {
// 解析失败时尝试解析为纯日期格式
try {
SimpleDateFormat dateOnlyFormat = new SimpleDateFormat("yyyyMMdd");
Date parsedDate = dateOnlyFormat.parse(dateTimeString);
return new SimpleDateFormat(pattern).format(parsedDate);
} catch (ParseException ex) {
// 无法解析时的异常处理
ex.printStackTrace();
return "解析失败";
}
}
}
}

View File

@ -146,7 +146,7 @@
})
queryInsuredCountByName(params).then(res => {
if (String(res.code) === '1') {
this.detail.insuredCount = res.data.insuredCount !== '0' ? res.data.insuredCount : '无数据';
this.detail.insuredCount = res.data.insuredCount !== '' ? res.data.insuredCount : '无数据';
}
}).catch(err => {
this.$message.error('请求出错了:' + err)