commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.core.util; |
|
26 |
|
|
27 |
import cn.hutool.core.date.DatePattern; |
|
28 |
import cn.hutool.core.date.DateTime; |
|
29 |
import cn.hutool.core.date.DateUnit; |
|
30 |
import cn.hutool.core.date.DateUtil; |
|
31 |
|
|
32 |
import java.util.Date; |
|
33 |
|
|
34 |
/** |
|
35 |
* 过去时间格式化工具类 |
|
36 |
* |
|
37 |
* @author xuyuxiang |
|
38 |
* @date 2020/8/6 14:29 |
|
39 |
**/ |
|
40 |
public class PastTimeFormatUtil { |
|
41 |
|
|
42 |
private static final long ONE_MINUTE_SECONDS = 60; |
|
43 |
|
|
44 |
private static final int BEFORE_DAWN_HOUR = 6; |
|
45 |
|
|
46 |
private static final int MORNING_END_HOUR = 12; |
|
47 |
|
|
48 |
private static final int NOON_END_HOUR = 13; |
|
49 |
|
|
50 |
private static final int AFTERNOON_END_HOUR = 18; |
|
51 |
|
|
52 |
private static final int NIGHT_END_HOUR = 24; |
|
53 |
|
|
54 |
/** |
|
55 |
* 将日期格式化为仿微信的日期 |
|
56 |
* |
|
57 |
* @param date 要格式化的日期 |
|
58 |
* @return 格式化结果 |
|
59 |
* @author xuyuxiang |
|
60 |
* @date 2020/8/6 11:41 |
|
61 |
**/ |
|
62 |
public static String formatPastTime(Date date) { |
|
63 |
if (DateUtil.between(date, DateUtil.date(), DateUnit.SECOND, false) < 0) { |
|
64 |
//今天之后的时间显示年月日时分 |
|
65 |
return DateUtil.format(date, DatePattern.NORM_DATETIME_MINUTE_PATTERN); |
|
66 |
} else { |
|
67 |
//如果是今年 |
|
68 |
if (DateUtil.thisYear() == DateUtil.year(date)) { |
|
69 |
//如果是今天 |
|
70 |
if (DateUtil.isSameDay(date, DateUtil.date())) { |
|
71 |
//相差分钟数 |
|
72 |
long betweenMinute = DateUtil.between(date, DateUtil.date(), DateUnit.MINUTE); |
|
73 |
//如果在1小时之内 |
|
74 |
if (betweenMinute < ONE_MINUTE_SECONDS) { |
|
75 |
//一分钟之内,显示刚刚 |
|
76 |
if (betweenMinute < 1) { |
|
77 |
return "刚刚"; |
|
78 |
} else { |
|
79 |
//一分钟之外,显示xx分钟前 |
|
80 |
return betweenMinute + "分钟前"; |
|
81 |
} |
|
82 |
} else { |
|
83 |
//一小时之外,显示时分 |
|
84 |
return getTodayHour(date) + " " + DateUtil.format(date, "HH:mm"); |
|
85 |
} |
|
86 |
} else if (DateUtil.isSameDay(date, DateUtil.yesterday())) { |
|
87 |
//如果是昨天,显示昨天时分 |
|
88 |
return "昨天 " + DateUtil.format(date, "HH:mm"); |
|
89 |
} else if (isThisWeek(date)) { |
|
90 |
//如果是本周 |
|
91 |
String weekday; |
|
92 |
//获取是本周的第几天 |
|
93 |
int dayOfWeek = DateUtil.dayOfWeek(date) - 1; |
|
94 |
switch (dayOfWeek) { |
|
95 |
case 1: |
|
96 |
weekday = "周一"; |
|
97 |
break; |
|
98 |
case 2: |
|
99 |
weekday = "周二"; |
|
100 |
break; |
|
101 |
case 3: |
|
102 |
weekday = "周三"; |
|
103 |
break; |
|
104 |
case 4: |
|
105 |
weekday = "周四"; |
|
106 |
break; |
|
107 |
case 5: |
|
108 |
weekday = "周五"; |
|
109 |
break; |
|
110 |
case 6: |
|
111 |
weekday = "周六"; |
|
112 |
break; |
|
113 |
default: |
|
114 |
weekday = "周日"; |
|
115 |
break; |
|
116 |
} |
|
117 |
//显示本周时分 |
|
118 |
return weekday + " " + DateUtil.format(date, "HH:mm"); |
|
119 |
} else { |
|
120 |
//否则显示月日时分 |
|
121 |
return DateUtil.format(date, "MM-dd HH:mm"); |
|
122 |
} |
|
123 |
} else { |
|
124 |
//本年之外显示年月日时分 |
|
125 |
return DateUtil.format(date, DatePattern.NORM_DATETIME_MINUTE_PATTERN); |
|
126 |
} |
|
127 |
} |
|
128 |
} |
|
129 |
|
|
130 |
/** |
|
131 |
* 判断日期是否是本周 |
|
132 |
* |
|
133 |
* @param date 要判断的日期 |
|
134 |
* @return boolean |
|
135 |
* @author xuyuxiang |
|
136 |
* @date 2020/8/6 12:10 |
|
137 |
**/ |
|
138 |
private static boolean isThisWeek(Date date) { |
|
139 |
//获取本周开始时间 |
|
140 |
DateTime beginOfWeek = DateUtil.beginOfWeek(DateUtil.date()); |
|
141 |
//获取与本周开始时间相差的天数 |
|
142 |
long betweenBegin = DateUtil.between(date, beginOfWeek, DateUnit.DAY, false) + 1; |
|
143 |
//如果是同一天,或相差天数小于0,则是本周 |
|
144 |
return DateUtil.isSameDay(date, beginOfWeek) || betweenBegin < 0; |
|
145 |
} |
|
146 |
|
|
147 |
/** |
|
148 |
* 根据今天日期获取早中晚 |
|
149 |
* |
|
150 |
* @author xuyuxiang |
|
151 |
* @date 2020/8/6 14:42 |
|
152 |
**/ |
|
153 |
private static String getTodayHour(Date date) { |
|
154 |
String result = ""; |
|
155 |
int hour = DateUtil.hour(date, true); |
|
156 |
if (hour >= 0 && hour <= BEFORE_DAWN_HOUR) { |
|
157 |
result = "凌晨"; |
|
158 |
} |
|
159 |
if (hour > BEFORE_DAWN_HOUR && hour < MORNING_END_HOUR) { |
|
160 |
result = "上午"; |
|
161 |
} |
|
162 |
if (hour == MORNING_END_HOUR) { |
|
163 |
result = "中午"; |
|
164 |
} |
|
165 |
if (hour >= NOON_END_HOUR && hour <= AFTERNOON_END_HOUR) { |
|
166 |
result = "下午"; |
|
167 |
} |
|
168 |
if (hour > AFTERNOON_END_HOUR && hour <= NIGHT_END_HOUR) { |
|
169 |
result = "晚上"; |
|
170 |
} |
|
171 |
return result; |
|
172 |
} |
|
173 |
} |