mac
2024-05-20 562ea59026c221ba2499c8b5cf70e838e44005dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
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;
import java.util.Date;
import java.util.Locale;
 
/**
 * 时间工具类
 */
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;
    }
 
    /**
     * 获取分钟
     *
     * @param timestamp 时间戳
     * @return -
     */
    public static String getMinuteTime(long timestamp) {
        try {
            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) {
            e.printStackTrace();
        }
        return "";
    }
 
    /**
     * 时间戳转时间(格式:yyyy-MM-dd HH:mm:ss)
     *
     * @param timestamp 时间戳
     * @return -
     */
    public static String getTimeFromTimestamp(long timestamp) {
        try {
            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) {
            e.printStackTrace();
        }
        return "";
    }
 
    /**
     * 时间戳转时间(格式:yyyy-MM-dd HH:mm:ss)
     *
     * @param timestamp 时间戳
     * @return -
     */
    @SuppressLint("DefaultLocale")
    public static String getCalendarTimeFromTimestamp(long timestamp) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(timestamp);
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始,所以要加1
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);
            int second = calendar.get(Calendar.SECOND);
            return String.format("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second);
        } catch (Exception e) {
            e.printStackTrace();
        }
        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;
    }
 
}