lmw
2025-04-24 718f31c92e2029d05260810435a2c70cef6e6ce5
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
package com.ypx.imagepicker.utils;
 
import android.annotation.SuppressLint;
import android.content.Context;
 
import com.ypx.imagepicker.R;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
 
/**
 * 时间工具类
 */
@SuppressLint("SimpleDateFormat")
public class PDateUtil {
 
    public static String getStrTime(Context context, long cc_time) {
        if (cc_time == 0) {
            return "";
        }
        if (String.valueOf(cc_time).length() <= 10) {
            cc_time = cc_time * 1000L;
        }
        Date date = new Date(cc_time);
        if (isToday(date)) {
            return context.getString(R.string.picker_str_today);
        }
        if (isThisWeek(date)) {
            return context.getString(R.string.picker_str_this_week);
        }
        if (isThisMonth(date)) {
            return context.getString(R.string.picker_str_this_months);
        }
        return new SimpleDateFormat(context.getString(R.string.picker_str_time_format)).format(date);
    }
 
    //判断选择的日期是否是本周
    private static boolean isThisWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(date);
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        return paramWeek == currentWeek;
    }
 
    //判断选择的日期是否是今天
    private static boolean isToday(Date date) {
        return isThisTime(date, "yyyy-MM-dd");
    }
 
    //判断选择的日期是否是本月
    private static boolean isThisMonth(Date date) {
        return isThisTime(date, "yyyy-MM");
    }
 
    private static boolean isThisTime(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String param = sdf.format(date);//参数时间
        String now = sdf.format(new Date());//当前时间
        return param.equals(now);
    }
 
 
    /**
     * 获取视频时长(格式化)
     */
    public static String getVideoDuration(long timestamp) {
        if (timestamp < 1000) {
            return "00:01";
        }
        Date date = new Date(timestamp);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
        return simpleDateFormat.format(date);
    }
 
 
    /*
     * 毫秒转化
     */
    public static String formatTime(Context context,Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;
 
        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
 
        StringBuilder sb = new StringBuilder();
        if (day > 0) {
            sb.append(day).append(context.getString(R.string.picker_str_day));
        }
        if (hour > 0) {
            sb.append(hour).append(context.getString(R.string.picker_str_hour));
        }
        if (minute > 0) {
            sb.append(minute).append(context.getString(R.string.picker_str_minute));
        }
        if (second > 0) {
            sb.append(second).append(context.getString(R.string.picker_str_second));
        }
        if (milliSecond > 0) {
            sb.append(milliSecond).append(context.getString(R.string.picker_str_milli));
        }
        return sb.toString();
    }
}