mac
2024-04-26 705006fe0c1187ace53ee6505f71ba98fe4b44e6
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
package com.hdl.photovoltaic.utils;
 
import android.annotation.SuppressLint;
 
import com.hdl.photovoltaic.config.UserConfigManage;
import com.hdl.photovoltaic.other.HdlThreadLogic;
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 zhDateFormat = "yyyy/MM/dd";
    public static final String enDateFormat = "dd/MM/yyyy";
 
    /**
     * 获取分钟
     *
     * @param timestamp 时间戳
     * @return -
     */
    public static String getMinuteTime(long timestamp) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("mm", Locale.ENGLISH);
            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, Locale.ENGLISH);
            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, Locale.ENGLISH);
            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 daysToAdd 天数
     * @return 日历
     */
    public static Calendar addDaysToDate(Calendar date, int daysToAdd) {
        Calendar newDate = (Calendar) date.clone();
        newDate.add(Calendar.DAY_OF_MONTH, daysToAdd);
        return newDate;
    }
 
    /**
     * 减天数
     *
     * @param date           日历
     * @param daysToSubtract 天数
     * @return 日历
     */
    public static Calendar subtractDaysFromDate(Calendar date, int daysToSubtract) {
        Calendar newDate = (Calendar) date.clone();
        newDate.add(Calendar.DAY_OF_MONTH, -daysToSubtract);
        return newDate;
    }
 
 
}