From b3d54bc25fd4cca49ec0854012d6ca9b14b72493 Mon Sep 17 00:00:00 2001 From: JIAL <2287346125@qq.com> Date: Mon, 15 Jan 2024 11:26:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0StringUtils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bizquery/controller/BizController.java | 6 +-- src/main/java/com/JIAL/utils/StringUtils.java | 39 +++++++++++++------ .../resources/templates/bizquery/index.html | 2 +- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/JIAL/demo/bizquery/controller/BizController.java b/src/main/java/com/JIAL/demo/bizquery/controller/BizController.java index e64ad0d..2dd42ea 100644 --- a/src/main/java/com/JIAL/demo/bizquery/controller/BizController.java +++ b/src/main/java/com/JIAL/demo/bizquery/controller/BizController.java @@ -77,9 +77,9 @@ public class BizController { Map 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: {}", "开始写日志文件"); diff --git a/src/main/java/com/JIAL/utils/StringUtils.java b/src/main/java/com/JIAL/utils/StringUtils.java index 65d9093..616abaf 100644 --- a/src/main/java/com/JIAL/utils/StringUtils.java +++ b/src/main/java/com/JIAL/utils/StringUtils.java @@ -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 "解析失败"; + } + } + } } diff --git a/src/main/resources/templates/bizquery/index.html b/src/main/resources/templates/bizquery/index.html index 6b371b8..5c2d23b 100644 --- a/src/main/resources/templates/bizquery/index.html +++ b/src/main/resources/templates/bizquery/index.html @@ -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)