首页 > 编程语言 >java模仿微博代码_Java相对时间转换 工具代码

java模仿微博代码_Java相对时间转换 工具代码

时间:2024-05-31 13:45:08浏览次数:26  
标签:3600 Java 代码 timeStr long 微博 between Date minute

    private String dateToRelativeTime(Date date) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date now = new Date();

        long between = (now.getTime() - date.getTime()) / 1000;//除以1000是为了转换成秒

        long day = between / (24 * 3600);

        long hour = between % (24 * 3600) / 3600;

        long minute = between % 3600 / 60;

        long second = between;

        String timeStr;

        if (day > 0 && day <= 29) {

            timeStr = day + "天前";

        } else if (day > 29) {

            timeStr = df.format(date);

        } else {

            if (hour > 0) {

                timeStr = hour + "小时前";

            } else {

                if (minute > 0) {

                    timeStr = minute + "分钟前";

                } else {

                    timeStr = second + "秒前";

                }

            }

        }

        return timeStr;

    }

 

标签:3600,Java,代码,timeStr,long,微博,between,Date,minute
From: https://www.cnblogs.com/liftsail/p/18224370

相关文章