服务器之家

服务器之家 > 正文

java判断今天,昨天,前天,不能用秒间隔的简单实例

时间:2020-08-28 10:44     来源/作者:Java教程网

实例如下:

?
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
一 json optString 解析的TimesTamp string
 
long dateSec = (long) (Double.valueOf(timestamp) * 1000);
 
三 Date date=new Date(dateSec);
 
四 getTimestampString(date);
 
public static String getTimestampString(Date paramDate) {
    String str = null;
    long l = paramDate.getTime();
    Calendar localCalendar = GregorianCalendar.getInstance();
    localCalendar.setTime(paramDate);
    int year = localCalendar.get(Calendar.YEAR);
    if(! isSameYear(year)){ //去年,直接返回
      String paramDate2str = new SimpleDateFormat("yyyy MM dd", Locale.CHINA).format(paramDate);
      return paramDate2str;
    }
 
 
    if (isSameDay(l)) {
      int i = localCalendar.get(Calendar.HOUR_OF_DAY);
      if (i > 17) {
        str = "晚上 HH:mm";//HH表示24小时,hh表示12小时进制,
      } else if ((i >= 0) && (i <= 6)) {
        str = "凌晨 HH:mm";
      } else if ((i > 11) && (i <= 17)) {
        str = "下午 HH:mm";
      } else {
        str = "上午 HH:mm";
      }
    } else if (isYesterday(l)) {
      str = "昨天 HH:mm";
    } else if (isBeforeYesterday(l)) {
      str = "前天 HH:mm";
    } else {
      str = "M月d日 HH:mm";
    }
    String paramDate2str = new SimpleDateFormat(str, Locale.CHINA).format(paramDate);
    return paramDate2str;
  }
 
。。。
 
//  获取 今天开始结束 时间
 
 public static TimeInfo getTodayStartAndEndTime() {
 
    Calendar localCalendar1 = Calendar.getInstance();
    localCalendar1.set(Calendar.HOUR_OF_DAY, 0);
    localCalendar1.set(Calendar.MINUTE, 0);
    localCalendar1.set(Calendar.SECOND, 0);
    localCalendar1.set(Calendar.MILLISECOND, 0);
    Date localDate1 = localCalendar1.getTime();
 
    long l1 = localDate1.getTime();
 
 
 
 
    Calendar localCalendar2 = Calendar.getInstance();
    localCalendar2.set(Calendar.HOUR_OF_DAY, 23);
    localCalendar2.set(Calendar.MINUTE, 59);
    localCalendar2.set(Calendar.SECOND, 59);
    localCalendar2.set(Calendar.MILLISECOND, 999);
    Date localDate2 = localCalendar2.getTime();
 
    long l2 = localDate2.getTime();
 
 
 
 
    TimeInfo localTimeInfo = new TimeInfo();
    localTimeInfo.setStartTime(l1);
    localTimeInfo.setEndTime(l2);
    return localTimeInfo;
 
  }
 
 
 
 
//  获取 昨天开始结束 时间
 
 
  public static TimeInfo getYesterdayStartAndEndTime() {
    Calendar localCalendar1 = Calendar.getInstance();
    localCalendar1.add(Calendar.DAY_OF_MONTH, -1);//5
    localCalendar1.set(Calendar.HOUR_OF_DAY, 0);//11
    localCalendar1.set(Calendar.MINUTE, 0);//12
    localCalendar1.set(Calendar.SECOND, 0);//13
    localCalendar1.set(Calendar.MILLISECOND, 0);//Calendar.MILLISECOND
    Date localDate1 = localCalendar1.getTime();
    long l1 = localDate1.getTime();
 
 
    Calendar localCalendar2 = Calendar.getInstance();
    localCalendar2.add(Calendar.DAY_OF_MONTH, -1);//5
    localCalendar2.set(Calendar.HOUR_OF_DAY, 23);//11
    localCalendar2.set(Calendar.MINUTE, 59);//12
    localCalendar2.set(Calendar.SECOND, 59);//13
    localCalendar2.set(Calendar.MILLISECOND, 999);//Calendar.MILLISECOND
    Date localDate2 = localCalendar2.getTime();
    long l2 = localDate2.getTime();
 
 
    TimeInfo localTimeInfo = new TimeInfo();
    localTimeInfo.setStartTime(l1);
    localTimeInfo.setEndTime(l2);
    return localTimeInfo;
  }
 
 
 
 
 
//  获取 前天开始结束 时间
 
 
  public static TimeInfo getBeforeYesterdayStartAndEndTime() {
    Calendar localCalendar1 = Calendar.getInstance();
    localCalendar1.add(Calendar.DAY_OF_MONTH, -2);
    localCalendar1.set(Calendar.HOUR_OF_DAY, 0);
    localCalendar1.set(Calendar.MINUTE, 0);
    localCalendar1.set(Calendar.SECOND, 0);
    localCalendar1.set(Calendar.MILLISECOND, 0);
    Date localDate1 = localCalendar1.getTime();
    long l1 = localDate1.getTime();
 
 
    Calendar localCalendar2 = Calendar.getInstance();
    localCalendar2.add(Calendar.DAY_OF_MONTH, -2);
    localCalendar2.set(Calendar.HOUR_OF_DAY, 23);
    localCalendar2.set(Calendar.MINUTE, 59);
    localCalendar2.set(Calendar.SECOND, 59);
    localCalendar2.set(Calendar.MILLISECOND, 999);
    Date localDate2 = localCalendar2.getTime();
    long l2 = localDate2.getTime();
    TimeInfo localTimeInfo = new TimeInfo();
    localTimeInfo.setStartTime(l1);
    localTimeInfo.setEndTime(l2);
    return localTimeInfo;
  }
 
 
 
 
 
  private static boolean isSameDay(long paramLong) {
    TimeInfo localTimeInfo = getTodayStartAndEndTime();
    return (paramLong > localTimeInfo.getStartTime()) && (paramLong < localTimeInfo.getEndTime());
  }
 
 
  private static boolean isYesterday(long paramLong) {
    TimeInfo localTimeInfo = getYesterdayStartAndEndTime();
    return (paramLong > localTimeInfo.getStartTime()) && (paramLong < localTimeInfo.getEndTime());
  }
 
 
 
 
 
  private static boolean isBeforeYesterday(long paramLong) {
    TimeInfo localTimeInfo = getBeforeYesterdayStartAndEndTime();
    return (paramLong > localTimeInfo.getStartTime()) && (paramLong < localTimeInfo.getEndTime());
  }
 
 
 
 
 
 
 
 
  public static boolean isSameYear(int year) {
    Calendar cal = Calendar.getInstance();
    int CurYear = cal.get(Calendar.YEAR);
//    Log.e("","CurYear="+CurYear);//2015
    return CurYear==year;
  }
 
 
。。。。。。
 
public class TimeInfo {
  private long startTime;
  private long endTime;
 
 
  public long getStartTime() {
    return this.startTime;
  }
 
 
  public void setStartTime(long paramLong) {
    this.startTime = paramLong;
  }
 
 
  public long getEndTime() {
    return this.endTime;
  }
 
 
  public void setEndTime(long paramLong) {
    this.endTime = paramLong;
  }
}

以上这篇java判断今天,昨天,前天,不能用秒间隔的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
返回顶部