From 30859ca8f2175475d2c666353bc27f3b2ceede53 Mon Sep 17 00:00:00 2001
From: mac <user@users-MacBook-Pro.local>
Date: 星期四, 22 八月 2024 13:43:47 +0800
Subject: [PATCH] 2024年08月22日13:43:45

---
 app/src/main/java/com/hdl/photovoltaic/utils/TimeUtils.java |  220 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 218 insertions(+), 2 deletions(-)

diff --git a/app/src/main/java/com/hdl/photovoltaic/utils/TimeUtils.java b/app/src/main/java/com/hdl/photovoltaic/utils/TimeUtils.java
index e0dbce4..fee374f 100644
--- a/app/src/main/java/com/hdl/photovoltaic/utils/TimeUtils.java
+++ b/app/src/main/java/com/hdl/photovoltaic/utils/TimeUtils.java
@@ -1,6 +1,13 @@
 package com.hdl.photovoltaic.utils;
 
 import android.annotation.SuppressLint;
+import android.text.TextUtils;
+
+import com.hdl.photovoltaic.config.UserConfigManage;
+import com.hdl.photovoltaic.enums.TimeType;
+import com.hdl.photovoltaic.other.HdlThreadLogic;
+import com.hdl.photovoltaic.ui.home.HomePageFragment;
+import com.hdl.sdk.link.core.utils.LanguageUtils;
 
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
@@ -12,6 +19,58 @@
  */
 public class TimeUtils {
 
+    public static final String zhDateYearMonthDayFormat = "yyyy/MM/dd";
+    public static final String zhDateYearMonthFormat = "yyyy/MM";
+    public static final String zhDateYearFormat = "yyyy";
+
+    public static final String enDateYearMonthDayFormat = "dd/MM/yyyy";
+    public static final String enDateYearMonthFormat = "MM/yyyy";
+    public static final String enDateYearFormat = "yyyy";
+
+    /**
+     * 鑾峰彇褰撳墠绯荤粺鏃ユ湡鏃堕棿鎴�
+     *
+     * @return 鏃堕棿鎴�
+     */
+    public static long getCurrentTimestamp() {
+        return new Date().getTime();
+    }
+
+    /**
+     * 鑾峰彇鏃ユ湡鏍煎紡 (2024/2/09,yyyy/DD/dd)
+     *
+     * @return 鏃ユ湡鏍煎紡 (2024/2/09,yyyy/DD/dd)
+     */
+    public static String getTimeDateFormat(String timeType) {
+        String timeDateFormat = TimeUtils.zhDateYearMonthDayFormat;
+        switch (timeType) {
+            case TimeType.day: {
+                timeDateFormat = TimeUtils.zhDateYearMonthDayFormat;
+                if (!UserConfigManage.getInstance().isZh()) {
+                    timeDateFormat = TimeUtils.enDateYearMonthDayFormat;
+                }
+            }
+            break;
+            case TimeType.month: {
+                timeDateFormat = TimeUtils.zhDateYearMonthFormat;
+                if (!UserConfigManage.getInstance().isZh()) {
+                    timeDateFormat = TimeUtils.enDateYearMonthFormat;
+                }
+            }
+            break;
+            case TimeType.year:
+            case TimeType.all: {
+                timeDateFormat = TimeUtils.zhDateYearFormat;
+                if (!UserConfigManage.getInstance().isZh()) {
+                    timeDateFormat = TimeUtils.enDateYearFormat;
+                }
+            }
+            break;
+
+        }
+        return timeDateFormat;
+    }
+
     /**
      * 鑾峰彇鍒嗛挓
      *
@@ -20,7 +79,25 @@
      */
     public static String getMinuteTime(long timestamp) {
         try {
-            SimpleDateFormat dateFormat = new SimpleDateFormat("mm", Locale.ENGLISH);
+            SimpleDateFormat dateFormat = new SimpleDateFormat("mm", getLocale());
+            Date date = new Date(timestamp);
+            return dateFormat.format(date);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return "";
+    }
+
+    /**
+     * 鏃堕棿鎴宠浆鏃堕棿(鏍煎紡:yyyy-MM-dd)
+     *
+     * @param timestamp   鏃堕棿鎴�
+     * @param date_format 鏃堕棿鏍煎紡(渚嬪"yyyy/MM/dd")
+     * @return -
+     */
+    public static String getDateTimestamp(long timestamp, String date_format) {
+        try {
+            SimpleDateFormat dateFormat = new SimpleDateFormat(date_format, getLocale());
             Date date = new Date(timestamp);
             return dateFormat.format(date);
         } catch (Exception e) {
@@ -37,7 +114,11 @@
      */
     public static String getTimeFromTimestamp(long timestamp) {
         try {
-            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
+            String pattern = "yyyy-MM-dd HH:mm";
+            if (UserConfigManage.getInstance().getCurrentAppLanguage().equals(LocalManageUtil.en)) {
+                pattern = "dd-MM-yyyy HH:mm";
+            }
+            SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, getLocale());
             Date date = new Date(timestamp);
             return dateFormat.format(date);
         } catch (Exception e) {
@@ -70,4 +151,139 @@
         return "";
     }
 
+    /**
+     * 鍔狅紙鏃ワ紝鏈堬紝骞达級璁$畻
+     *
+     * @param date              鏃ュ巻
+     * @param dayMonthYearToAdd 澶╂暟锛堟棩锛屾湀锛屽勾锛�
+     * @return 鏃ュ巻
+     */
+    public static String addDayMonthYearToDate(Calendar date, int dayMonthYearToAdd, String dateFormat, String timeType) {
+        Calendar newDate = (Calendar) date.clone();
+        switch (timeType) {
+            case TimeType.day: {
+                newDate.add(Calendar.DAY_OF_MONTH, dayMonthYearToAdd);
+            }
+            break;
+            case TimeType.month: {
+                newDate.add(Calendar.MONTH, dayMonthYearToAdd);
+            }
+            break;
+            case TimeType.year:
+            case TimeType.all: {
+                newDate.add(Calendar.YEAR, dayMonthYearToAdd);
+            }
+            break;
+        }
+        return calendarToString(newDate, dateFormat);
+    }
+
+    /**
+     * 鍑忥紙鏃ワ紝鏈堬紝骞达級璁$畻
+     *
+     * @param date                   鏃ュ巻
+     * @param dayMonthYearToSubtract 澶╂暟
+     * @return 鏃ュ巻
+     */
+    public static String subtractDayMonthYearFromDate(Calendar date, int dayMonthYearToSubtract, String dateFormat, String timeType) {
+        Calendar newDate = (Calendar) date.clone();
+        switch (timeType) {
+            case TimeType.day: {
+                newDate.add(Calendar.DAY_OF_MONTH, -dayMonthYearToSubtract);
+            }
+            break;
+            case TimeType.month: {
+                newDate.add(Calendar.MONTH, -dayMonthYearToSubtract);
+            }
+            break;
+            case TimeType.year:
+            case TimeType.all: {
+                newDate.add(Calendar.YEAR, -dayMonthYearToSubtract);
+            }
+            break;
+        }
+        return calendarToString(newDate, dateFormat);
+    }
+
+    /**
+     * 鏃堕棿鎴宠浆Calendar
+     *
+     * @param timestamp 鏃堕棿鎴�
+     * @return Calendar瀵硅薄
+     */
+    public static Calendar timestampToCalendar(long timestamp) {
+        // 灏嗘椂闂存埑杞崲涓烘棩鏈�
+        Date date = new Date(timestamp);
+        // 浣跨敤Calendar绫昏缃棩鏈�
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(date);
+        return calendar;
+    }
+
+    /**
+     * 瀛楃涓叉棩鏈熸牸寮忚浆Calendar
+     *
+     * @param dateString (2023/04/01)
+     * @param dateFormat (yyyy/MM/dd)
+     * @return Calendar瀵硅薄
+     */
+    public static Calendar stringToCalendar(String dateString, String dateFormat) {
+        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, getLocale());
+        Calendar calendar = Calendar.getInstance();
+        try {
+            Date date = sdf.parse(dateString);
+            if (date != null) {
+                calendar.setTime(date);
+            }
+        } catch (Exception ignored) {
+        }
+        return calendar;
+    }
+
+    /**
+     * Calendar杞瓧绗︿覆鏃ユ湡鏍煎紡
+     *
+     * @param dateFormat (yyyy/MM/dd)
+     * @return Calendar瀵硅薄
+     */
+    public static String calendarToString(Calendar calendar, String dateFormat) {
+        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, getLocale());
+        return sdf.format(calendar.getTime());
+    }
+
+    /**
+     * Date杞瓧绗︿覆鏃ユ湡鏍煎紡
+     *
+     * @param dateFormat (yyyy/MM/dd)
+     * @return Calendar瀵硅薄
+     */
+    public static String dateToString(Date date, String dateFormat) {
+        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, getLocale());
+        return sdf.format(date.getTime());
+    }
+
+    /**
+     * 瀛楃涓叉棩鏈熸牸寮忚浆鏃堕棿鎴�
+     *
+     * @param dateString (2024/06/01)
+     * @param dateFormat (yyyy/MM/dd)
+     * @return 鏃堕棿鎴�
+     */
+    public static long stringDateToTimestamp(String dateString, String dateFormat) {
+        try {
+            SimpleDateFormat slf = new SimpleDateFormat(dateFormat, getLocale());
+            Date date = slf.parse(dateString);
+            if (date == null) {
+                return 0;
+            }
+            return date.getTime(); // 鑾峰彇鏃堕棿鎴�
+        } catch (Exception ignored) {
+        }
+        return 0;
+    }
+
+    private static Locale getLocale() {
+        return Locale.ENGLISH;
+    }
+
 }

--
Gitblit v1.8.0