mac
2023-12-01 593303fa91fd541844b204ec913c462d7855a31e
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
package com.hdl.photovoltaic.utils;
 
import android.annotation.SuppressLint;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
 
/**
 * 时间工具类
 */
public class TimeUtils {
 
    /**
     * 时间戳转时间(格式:yyyy-MM-dd HH:mm:ss)
     *
     * @param timestamp 时间戳
     * @return -
     */
    public static String getTimeFromTimestamp(long timestamp) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", 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 "";
    }
 
}