rk
昨天 4a8ff39b0fab0627ef8f7459587d514cc01c3676
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
package com.doumee.lib_coremodel.util;
 
import android.app.Activity;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
 
/**
 * Created by MSI on 2018/4/18.
 */
 
public class TimeUtils {
 
    public static String stringToString(String oldTime){
        long old=0;
        try {
            old= Long.parseLong(oldTime);
        }catch (NumberFormatException e){
            return "";
        }
        long time= (System.currentTimeMillis() - old)/1000;
        long sec = time/60;
        if (sec<60) {
            if(sec<=0){
                return "刚刚";
            }else{
                return String.format("%d分钟前",sec);
            }
        }
        // 秒转小时
        long hours = time/3600;
        if (hours<24) {
            return String.format("%d小时前",hours);
        }
        //秒转天数
        long days = time/3600/24;
        if (days < 30) {
            return String.format("%d天前",days);
        }
        //秒转月
        long months = time/3600/24/30;
        if (months < 12) {
            return String.format("%d月前",months);
        }
        //秒转年
        long years = time/3600/24/30/12;
        return String.format("%d年前",years);
 
    }
 
    public static String longToString(long oldTime){
        long time= (System.currentTimeMillis() - oldTime)/1000;
        long sec = time/60;
        if (sec<60) {
            if(sec<=0){
                return "刚刚";
            }else{
                return String.format("%d分钟前",sec);
            }
        }
        // 秒转小时
        long hours = time/3600;
        if (hours<24) {
            return String.format("%d小时前",hours);
        }
        //秒转天数
        long days = time/3600/24;
        if (days < 30) {
            return String.format("%d天前",days);
        }
        //秒转月
        long months = time/3600/24/30;
        if (months < 12) {
            return String.format("%d月前",months);
        }
        //秒转年
        long years = time/3600/24/30/12;
        return String.format("%d年前",years);
 
    }
 
    public static String getDayAndWeek(Calendar calendar){
        String[] weekList = new String[]{"", "周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        String weekStr = weekList[calendar.get(Calendar.DAY_OF_WEEK)];
        DateFormat df = new SimpleDateFormat("yyyy年");
        return df.format(calendar.getTime())+calendar.get(Calendar.MONTH)+"月"+calendar.get(Calendar.DAY_OF_MONTH)+"日 "+weekStr;
    }
 
    public static String getTime(Calendar calendar, Activity activity){
        boolean is24HourFormat = android.text.format.DateFormat.is24HourFormat(activity);
 
        //判断时间制
        String ampm = "";
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if(is24HourFormat) {
            //24小时制
            if (hour < 12) {
                ampm = "上午";
            } else {
                ampm = "下午";
            }
        }else {
            //12小时制
            if(calendar.get(Calendar.AM_PM)==0){
                ampm = "上午";
            }else {
                ampm = "下午";
            }
        }
        if(hour>12){
            hour-=12;
        }
        return ampm+hour+":"+new SimpleDateFormat("mm").format(calendar.getTime());
    }
}