| | |
| | | import java.io.IOException;
|
| | | import java.io.PrintWriter;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Calendar;
|
| | | import java.util.Date;
|
| | | import java.util.List;
|
| | |
|
| | |
| | | public void write(JsonWriter out, Date value) throws IOException {
|
| | | String desc = "";
|
| | | if (value != null) {
|
| | | // 判断是否是同一天
|
| | |
|
| | | Calendar calendar = Calendar.getInstance();
|
| | | calendar.setTime(value);
|
| | | int y1 = calendar.get(Calendar.YEAR);// 获取年份
|
| | | int d1 = calendar.get(Calendar.DAY_OF_YEAR);// 获取年中第几天
|
| | |
|
| | | Date nowDate = new Date();
|
| | | Calendar calendar2 = Calendar.getInstance();
|
| | | calendar2.setTime(nowDate);
|
| | | int y2 = calendar2.get(Calendar.YEAR);// 获取年份
|
| | | int d2 = calendar2.get(Calendar.DAY_OF_YEAR);// 获取年中第几天
|
| | |
|
| | | long old = value.getTime();
|
| | | long now = System.currentTimeMillis();
|
| | | long oldDay = old / (1000 * 60 * 60 * 24L);
|
| | | long nowDay = now / (1000 * 60 * 60 * 24L);
|
| | | if (oldDay == nowDay) {// 同一天
|
| | | long cha = now - old;
|
| | | if (cha < 1000 * 60 * 2L)
|
| | | desc = "刚刚";
|
| | | else if (cha < 1000 * 60 * 60L)
|
| | | desc = (cha / (1000 * 60)) + "分钟前";
|
| | | else
|
| | | desc = (cha / (1000 * 60 * 60)) + "小时前";
|
| | | } else if (nowDay - oldDay == 1) {
|
| | | desc = "昨天";
|
| | | long now = nowDate.getTime();
|
| | | if (y1 == y2) {
|
| | | if (d1 == d2) {
|
| | | long cha = now - old;
|
| | | if (cha < 1000 * 60 * 2L) {
|
| | | desc = "刚刚";
|
| | | }else if (cha < 1000 * 60 * 60L) {
|
| | | desc = (cha / (1000 * 60)) + "分钟前";
|
| | | }else {
|
| | | desc = (cha / (1000 * 60 * 60)) + "小时前";
|
| | | }
|
| | | } else if (d2 - d1 == 1) {
|
| | | desc = "昨天";
|
| | | } else {
|
| | | desc = (d2 - d1) + "天前";
|
| | | }
|
| | | } else {
|
| | | desc = (nowDay - oldDay) + "天前";
|
| | | int timeDistance = 0;
|
| | | for (int i = y1; i < y2; i++) {
|
| | | if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
|
| | | timeDistance += 366; // 闰年
|
| | | } else {
|
| | | timeDistance += 365; // 不是闰年
|
| | | }
|
| | | }
|
| | | desc = timeDistance + (d2 - d1) + "天前";
|
| | | }
|
| | |
|
| | | out.value(desc);
|
| | | }
|
| | | }
|