lmw
2023-04-03 16ea883d3c03fd8b910f9282aa1bc08378d40d54
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
package com.zhaoyang.driver.utils;
 
import android.annotation.SuppressLint;
import android.text.TextUtils;
 
import androidx.annotation.StringDef;
 
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
 
/**
 * Created by Administrator on 2017/3/24.
 */
 
@SuppressLint("SimpleDateFormat")
public class DateUtil {
 
 
    public static String getTime(@TYPE String type, long time) {
        return getTime(type, time, false);
    }
 
    public static String splitDate(String date) {
        if (TextUtils.isEmpty(date)) return date;
        String[] split = date.split(" ");
        if (split.length > 1) return split[0];
        return date;
    }
 
 
    /**
     * 30分钟以内的显示【刚刚】;
     * 30分钟至1个小时的显示【30分钟】;
     * 1个小时到2个小时的为【1小时】;
     * 2个小时以上且是今天的,展示为【今天】;
     * 其余的展示发货日期。
     *
     * @param type
     * @param time
     * @param isCustom 自定义 返回
     * @return
     */
    public static String getTime(@TYPE String type, long time, boolean isCustom) {
        SimpleDateFormat format;
        long differTime = (System.currentTimeMillis() - time) / (60 * 1000);
        long currentTime = System.currentTimeMillis();
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTimeInMillis(currentTime);
        long productTime = Long.valueOf(time);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTimeInMillis(productTime);
        if (isCustom) {
            if (differTime < 60 && differTime >= 0) {
                return differTime+"分钟前"; //"1小时内"
            } else if (differTime < 24 * 60 && calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH)) {
                return differTime/60+"小时前";//今天
            } else if (differTime < 24 * 60 && calendar1.get(Calendar.DAY_OF_MONTH) != calendar2.get(Calendar.DAY_OF_MONTH)) {
                return "昨天";
            } else if (differTime >=  24 * 60 && differTime < 60 * 24 * 2) {
                return "前天";//今天
            }else {
                format = new SimpleDateFormat(type);
                return format.format(new Date(time));
            }
        } else {
            format = new SimpleDateFormat(type);
            return format.format(new Date(time));
        }
    }
 
    /***
     * 返回秒
     * @param time
     * @return
     */
    public static String getDifferTime(long time) {
        long differTime = time / (1000);
        long f = differTime / 60;
        long m = differTime % 60;
        if (f == 0) {
            return m + "";
        } else {
            return f + ":" + m;
        }
    }
 
    /*
     * 将时间转换为时间戳
     */
    public static long dateToStamp(@TYPE String type, String s) throws ParseException {
        if (TextUtils.isEmpty(s)) return new Date().getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(type);
        Date date = simpleDateFormat.parse(s);
        return date.getTime();
    }
 
    public static final String TYPE0 = "yyyy-MM-dd HH:mm:ss";
    public static final String TYPE1 = "yyyy-MM-dd";
    public static final String TYPE2 = "yyyy年MM月dd日 HH:mm:ss ";
    public static final String TYPE3 = "yyyy/MM/dd";
    public static final String TYPE4 = "yyyy-MM-dd HH:mm";
    public static final String TYPE5 = "HH:mm";
    public static final String TYPE6 = "dd号 HH:mm";
    public static final String TYPE7 = "MM月dd日";
    public static final String TYPE8 = "yyyy/MM/dd HH:mm:ss ";
    public static final String TYPE9 = "MM月dd日 HH:mm";
 
    @StringDef({
            TYPE0,
            TYPE1,
            TYPE2,
            TYPE3,
            TYPE4,
            TYPE5,
            TYPE6,
            TYPE7,
            TYPE8,
            TYPE9
    })
    @Retention(RetentionPolicy.SOURCE)
    @interface TYPE {
    }
 
    public static String getAstro(int Calendar_month, int Calendar_day) {
        String[] astro = new String[]{"摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座",
                "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座"};
        int[] arr = new int[]{20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22};// 两个星座分割日
        int index = Calendar_month;
        // 所查询日期在分割日之前,索引-1,否则不变
        if (Calendar_day < arr[Calendar_month - 1]) {
            index = index - 1;
        }
        return astro[index];
    }
 
    public static String getWeek(long time){
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar calendar=Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return weekDays[calendar.get(Calendar.DAY_OF_WEEK)-1];
    }
}