java 友好时间显示_以友好的方式展示时间

package com.industry.base.util;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.Locale;

/**

* 以友好的方式展示时间

*

* @author z

* @date 2018/3/31 13:53

*/

public class TimeFriendlyUtil {

private final static ThreadLocal dateFormater2 = new ThreadLocal() {

@Override

protected SimpleDateFormat initialValue() {

return new SimpleDateFormat("yyyy-MM-dd");

}

};

private static final int SEC = 1000;

private static final int MIN = 60000;

private static final int HOUR = 3600000;

private static final int DAY = 86400000;

/**

* 以友好的方式显示时间

*

* @param dateTime 时间

* @return 友好的时间格式

*/

public static String friendlyTime(Date dateTime) {

if (dateTime == null) {

return "Unknown";

}

if (System.currentTimeMillis() > dateTime.getTime()) {

return friendlyTimeBefore(dateTime);

}

return friendlyTimeAfter(dateTime);

}

/**

* 以友好的方式显示时间(yyyy-MM-dd)

*

* @param dateTime 时间

* @return 有好的时间表达格式

*/

public static String friendlyTimeBefore(Date dateTime) {

if (dateTime == null) {

return "Unknown";

}

String ftime = "";

Calendar cal = Calendar.getInstance();

long lt = dateTime.getTime();

long ct = cal.getTimeInMillis();

int days = DateUtil.daysBetween(dateTime, new Date());

if (days == 0) {

int hour = (int) ((cal.getTimeInMillis() - dateTime.getTime()) / 3600000);

if (hour == 0) {

ftime = Math.max((cal.getTimeInMillis() - dateTime.getTime()) / 60000, 1) + "分钟前";

} else {

ftime = "今天" + DateUtil.date2String(dateTime, "HH:mm");

}

} else if (days == 1) {

ftime = "昨天" + DateUtil.date2String(dateTime, "HH:mm");

} else if (days > 1) {

ftime = dateFormater2.get().format(dateTime);

}

return ftime;

}

/**

* 以友好的方式显示时间(之后)

*

* @param dateTime 时间

* @return 友好的方式显示时间

*/

public static String friendlyTimeAfter(Date dateTime) {

if (dateTime == null) {

return "Unknown";

}

String ftime = "";

Calendar cal = Calendar.getInstance();

// 判断是否是同一天

String curDate = dateFormater2.get().format(cal.getTime());

String paramDate = dateFormater2.get().format(dateTime);

if (curDate.equals(paramDate)) {

int hour = (int) ((dateTime.getTime() - cal.getTimeInMillis()) / 3600000);

if (hour == 0) {

ftime = Math.max((dateTime.getTime() - cal.getTimeInMillis()) / 60000, 1) + "分钟后";

} else {

ftime = hour + "小时后";

}

return ftime;

}

long lt = dateTime.getTime() / 86400000;

long ct = cal.getTimeInMillis() / 86400000;

int days = (int) (lt - ct);

if (days == 0) {

int hour = (int) ((dateTime.getTime() - cal.getTimeInMillis()) / 3600000);

if (hour == 0) {

ftime = Math.max((dateTime.getTime() - cal.getTimeInMillis()) / 60000, 1) + "分钟后";

} else {

ftime = hour + "小时后";

}

} else if (days == 1) {

ftime = "明天";

} else if (days == 2) {

ftime = "后天";

} else if (days > 2 && days <= 10) {

ftime = days + "天后";

} else if (days > 10) {

ftime = dateFormater2.get().format(dateTime);

}

return ftime;

}

/**

* 友好时间

*

* @param millis 时间戳

* @return 友好时间

*/

public static String getFriendlyTimeSpanByNow(long millis) {

long now = System.currentTimeMillis();

long span = now - millis;

if (span < 0) {

// U can read http://www.apihome.cn/api/java/Formatter.html to understand it.

return String.format("%tc", millis);

}

if (span < SEC) {

return "刚刚";

} else if (span < MIN) {

return String.format(Locale.getDefault(), "%d秒前", span / SEC);

} else if (span < HOUR) {

return String.format(Locale.getDefault(), "%d分钟前", span / MIN);

}

// 获取当天 00:00

long wee = getWeeOfToday();

if (millis >= wee) {

return String.format("今天%tR", millis);

} else if (millis >= wee - DAY) {

return String.format("昨天%tR", millis);

} else {

return String.format("%tF", millis);

}

}

/**

* 获取当天 00:00

*

* @return 当天 00:00时间戳

*/

private static long getWeeOfToday() {

Calendar cal = Calendar.getInstance();

cal.set(Calendar.HOUR_OF_DAY, 0);

cal.set(Calendar.SECOND, 0);

cal.set(Calendar.MINUTE, 0);

cal.set(Calendar.MILLISECOND, 0);

return cal.getTimeInMillis();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值