From 8113d635cf7a94402ee16a280a7aa10895494ef5 Mon Sep 17 00:00:00 2001 From: junjun Date: Wed, 28 Feb 2024 13:50:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96calcite=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=87=BD=E6=95=B0=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/dataease/engine/utils/DateUtils.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 core/core-backend/src/main/java/io/dataease/engine/utils/DateUtils.java diff --git a/core/core-backend/src/main/java/io/dataease/engine/utils/DateUtils.java b/core/core-backend/src/main/java/io/dataease/engine/utils/DateUtils.java new file mode 100644 index 0000000000..24959eb3f6 --- /dev/null +++ b/core/core-backend/src/main/java/io/dataease/engine/utils/DateUtils.java @@ -0,0 +1,79 @@ +package io.dataease.engine.utils; + +import java.text.SimpleDateFormat; + +/** + * @Author Junjun + */ +public class DateUtils { + private static final String format = "yyyy-MM-dd HH:mm:ss"; + private static final String minuteFormat = "yyyy-MM-dd HH:mm"; + private static final String hourFormat = "yyyy-MM-dd HH"; + private static final String dateOnly = "yyyy-MM-dd"; + private static final String monthOnly = "yyyy-MM"; + private static final String yearOnly = "yyyy"; + private static final String timeOnly = "HH:mm:ss"; + + public static String get_date_format(String date) { + // check date split '-' or '/' + String format1 = format; + String minuteFormat1 = minuteFormat; + String hourFormat1 = hourFormat; + String timeOnly1 = timeOnly; + String dateOnly1 = dateOnly; + String monthOnly1 = monthOnly; + String yearOnly1 = yearOnly; + if (date != null && date.contains("/")) { + format1 = format1.replaceAll("-", "/"); + minuteFormat1 = minuteFormat1.replaceAll("-", "/"); + hourFormat1 = hourFormat1.replaceAll("-", "/"); + timeOnly1 = timeOnly1.replaceAll("-", "/"); + dateOnly1 = dateOnly1.replaceAll("-", "/"); + monthOnly1 = monthOnly1.replaceAll("-", "/"); + yearOnly1 = yearOnly1.replaceAll("-", "/"); + } + try { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format1); + simpleDateFormat.parse(date); + return format1; + } catch (Exception e) { + } + try { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(minuteFormat1); + simpleDateFormat.parse(date); + return minuteFormat1; + } catch (Exception e) { + } + try { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(hourFormat1); + simpleDateFormat.parse(date); + return hourFormat1; + } catch (Exception e) { + } + try { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeOnly1); + simpleDateFormat.parse(date); + return timeOnly1; + } catch (Exception e) { + } + try { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateOnly1); + simpleDateFormat.parse(date); + return dateOnly1; + } catch (Exception e) { + } + try { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(monthOnly1); + simpleDateFormat.parse(date); + return monthOnly1; + } catch (Exception e) { + } + try { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat(yearOnly1); + simpleDateFormat.parse(date); + return yearOnly1; + } catch (Exception e) { + } + return format1; + } +}