• 以友好的方式展示时间


    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<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
    @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();
    }

    }

  • 相关阅读:
    Liferay7 BPM门户开发之34: liferay7对外服务类生成(RestService Get Url)
    Liferay7 BPM门户开发之33: Portlet之间通信的3种方式(session、IPC Render Parameter、IPC Event、Cookies)
    Liferay7 BPM门户开发之32: 实现自定义认证登陆(定制Authentication Hook)
    Liferay7 BPM门户开发之30: 通用帮助类Validator、ArrayUtil、StringUtil等使用
    Liferay7 BPM门户开发之29: 核心kernel.util包下面的通用帮助类ParamUtil、GetterUtil使用
    Liferay7 BPM门户开发之28: Portlet文件上传,及实体类同步更新上传
    Liferay7 BPM门户开发之26: 集成Activiti到Liferay7
    Liferay7 BPM门户开发之27: MVC Portlet插件工程开发
    Liferay7 BPM门户开发之25: Liferay7应用程序配置(APPLICATION CONFIGURATION)
    Liferay7 BPM门户开发之24: Liferay7应用程序安全
  • 原文地址:https://www.cnblogs.com/04241202-nan/p/11654423.html
Copyright © 2020-2023  润新知