From 9b790d284324a54bd54de12a336e488e748a2b61 Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Thu, 12 Aug 2021 14:57:15 +0800
Subject: [PATCH] 监测因子趋势图时间精简

---
 screen-common/src/main/java/com/moral/util/DateUtils.java |   79 +++++++++++++++++++++++++++++++++++++--
 1 files changed, 75 insertions(+), 4 deletions(-)

diff --git a/screen-common/src/main/java/com/moral/util/DateUtils.java b/screen-common/src/main/java/com/moral/util/DateUtils.java
index 1d777ef..51034a8 100644
--- a/screen-common/src/main/java/com/moral/util/DateUtils.java
+++ b/screen-common/src/main/java/com/moral/util/DateUtils.java
@@ -16,6 +16,10 @@
 
 public class DateUtils {
     /**
+     * ������������(yyyy)
+     */
+    public static final String yyyy = "yyyy";
+    /**
      * ������������(yyyy-MM-dd)
      */
     public static final String yyyy_MM_dd_EN = "yyyy-MM-dd";
@@ -746,8 +750,8 @@
      * @param days
      * @return
      */
-    public static String getDateAddDay(String date, int days, String format) {
-        DateFormat df = getDateFormat(format);
+    public static String getDateAddDay(String date, int days) {
+        DateFormat df = getDateFormat(yyyy_MM_dd_EN);
         try {
             Calendar cal = Calendar.getInstance();
             cal.setTime(df.parse(date));
@@ -791,7 +795,7 @@
      * @return
      */
     public static String getDateAddMonth(String date, int m) {
-        DateFormat df = getDateFormat(yyyyMM_EN);
+        DateFormat df = getDateFormat(yyyy_MM_EN);
         try {
             Calendar cal = Calendar.getInstance();
             cal.setTime(df.parse(date));
@@ -1337,7 +1341,74 @@
         return getDate(dateToDateString(calendar.getTime(), yyyy_MM_dd_EN));
     }
 
+
+    /*���������������������*/
+    public static String getDateAddYear(String date, int year) {
+        DateFormat df = getDateFormat(yyyy);
+        try {
+            Calendar cal = Calendar.getInstance();
+            cal.setTime(df.parse(date));
+            cal.add(Calendar.YEAR, year);
+            date = df.format(cal.getTime());
+        } catch (ParseException e) {
+            throw new RuntimeException(e);
+        }
+        return date;
+    }
+
+    /*
+     * ������������������������������������
+     * ������time=2021-08-04 ���������������������������������������
+     *    time=2021-08 ���������������������������������
+     * */
+    public static List<String> getTimeLag(String time) {
+        List<String> result = new ArrayList<>();
+        int length = time.length();
+        Calendar cal = Calendar.getInstance();
+        String end;
+        String dateFormat;
+        String df;
+        int i;
+        if (length == 10) {//���
+            end = getDateAddDay(time, 1);
+            dateFormat = yyyy_MM_dd_HH_EN;
+            df = yyyy_MM_dd_EN;
+            i = Calendar.HOUR_OF_DAY;
+        } else if (length == 7) {//���
+            end = getDateAddMonth(time, 1);
+            dateFormat = yyyy_MM_dd_EN;
+            df = yyyy_MM_EN;
+            i = Calendar.DAY_OF_MONTH;
+        } else {//���
+            end = getDateAddYear(time, 1);
+            dateFormat = yyyy_MM_EN;
+            df = yyyy;
+            i = Calendar.MONTH;
+        }
+        cal.setTime(getDate(time, df));
+        for (long d = cal.getTimeInMillis(); d < getDate(end, df).getTime(); cal.set(i, cal.get(i) + 1), d = cal.getTimeInMillis()) {
+            String format = dateToDateString(new Date(d),dateFormat);
+            result.add(format);
+        }
+        return result;
+    }
+
+    //������date���������������������������0���5���������������������2021-08-09 15:32:00->2021-08-09 15:30:00���2021-08-09 15:39:00->2021-08-09 15:35:00
+    public static Date getFiveMinuteDate(Date date) {
+        String dateString = dateToDateString(date, yyyy_MM_dd_HH_mm_EN);
+        String minute = dateString.substring(15, 16);
+        int i = Integer.parseInt(minute);
+        if (i > 0 && i < 5) {
+            i = 0;
+        } else if (i > 5 && i <= 9) {
+            i = 5;
+        }
+        StringBuilder stringBuffer = new StringBuilder(dateString);
+        stringBuffer.replace(15, 16, String.valueOf(i));
+        return getDate(stringBuffer.toString(), yyyy_MM_dd_HH_mm_EN);
+    }
+
     public static void main(String[] args) {
-        System.out.println(getFirstDayOfCurrMonth());
+        System.out.println(getTimeLag("2021"));
     }
 }

--
Gitblit v1.8.0