| | |
| | | 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; |
| | |
| | | */ |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * 获取分钟 |
| | | * |
| | |
| | | */ |
| | | 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) { |
| | |
| | | */ |
| | | 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) { |
| | |
| | | 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; |
| | | } |
| | | |
| | | } |