java时间换算工具类

文章目录


一、时间换算工具类DateUtils

java 复制代码
public class DateUtils {

    /**
     * 获取时间描述
     *
     * @param inTime
     * @return
     */
    public static String timeConversion(Date inTime) {
        // 拿到当前时间戳和发布时的时间戳,然后得出时间戳差
        Date curTime = new Date();
        long timeDiff = curTime.getTime() - inTime.getTime();
        //上面一行代码可以换成以下(兼容性的解决)

        // 单位换算
        long min = 60 * 1000;
        long hour = min * 60;
        long day = hour * 24;
        long week = day * 7;
        long month = day * 30;
        long year = month * 12;
        DecimalFormat df = new DecimalFormat("#");
        // 计算发布时间距离当前时间的周、天、时、分
        double exceedyear = Math.floor(timeDiff / year);
        double exceedmonth = Math.floor(timeDiff / month);
        double exceedWeek = Math.floor(timeDiff / week);
        double exceedDay = Math.floor(timeDiff / day);
        double exceedHour = Math.floor(timeDiff / hour);
        double exceedMin = Math.floor(timeDiff / min);


        // 最后判断时间差到底是属于哪个区间,然后return

        if (exceedyear < 100 && exceedyear > 0) {
            return df.format(exceedyear) + "年前";
        } else {
            if (exceedmonth < 12 && exceedmonth > 0) {
                return df.format(exceedmonth) + "月前";
            } else {
                if (exceedWeek <= 4 && exceedWeek > 0) {
                    return df.format(exceedWeek) + "星期前";
                } else {
                    if (exceedDay < 7 && exceedDay > 0) {
                        return df.format(exceedDay) + "天前";
                    } else {
                        if (exceedHour < 24 && exceedHour > 0) {
                            return df.format(exceedHour) + "小时前";
                        } else {
                            return df.format(exceedMin) + "分钟前";
                        }
                    }
                }
            }
        }
    }

    public static String getDurationTime(long timeDiff) {
        Duration duration = Duration.ofMillis(timeDiff);
        // 单位换算
        long days = duration.toDays();
        long hours = duration.toHours() % 24;
        long minutes = duration.toMinutes() % 60;
        long seconds = duration.getSeconds() % 60;

        String result = "";

        if (days > 0) {
            result += days + "天";
        }

        if (hours > 0) {
            result += hours + "小时";
        }

        if (minutes > 0) {
            result += minutes + "分钟";
        }

        if (seconds > 0) {
            result += seconds + "秒";
        }

        if (result.equals("")) {
            result = "0秒";
        }
        return result;
    }

    /**
     * 时间字符串转时间格式
     *
     * @param strDate
     * @return
     */
    public static Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        return formatter.parse(strDate, pos);
    }

    public static LocalDateTime isoConvertToLocal(String isoTime) {
        if(StringUtils.isBlank(isoTime)){
            return null;
        }
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(isoTime);
        ZoneId zoneId = ZoneId.systemDefault();
        return zonedDateTime.withZoneSameInstant(zoneId).toLocalDateTime();
    }

    /**
     * 获取两个时间段的时间差:格式:%d天%d小时%d分%d秒%d毫秒
     *
     * @param execStartTime
     * @param execEndTime
     * @return
     */

    public static String getDuration(LocalDateTime execStartTime, LocalDateTime execEndTime) {
        Duration duration = Duration.between(execStartTime, execEndTime);
        // 计算各个时间单位的差值
        long days = duration.toDays();
        long hours = duration.toHours() % 24;
        long minutes = duration.toMinutes() % 60;
        long seconds = duration.getSeconds() % 60;
        long millis = duration.toMillis() % 1000;
// 格式化为字符串
        String formattedDuration = String.format("%d天%d小时%d分%d秒%d毫秒", days, hours, minutes, seconds, millis);
        return formattedDuration;
    }
相关推荐
熊小猿34 分钟前
在 Spring Boot 项目中使用分页插件的两种常见方式
java·spring boot·后端
paopaokaka_luck38 分钟前
基于SpringBoot+Vue的助农扶贫平台(AI问答、WebSocket实时聊天、快递物流API、协同过滤算法、Echarts图形化分析、分享链接到微博)
java·vue.js·spring boot·后端·websocket·spring
老华带你飞1 小时前
机器人信息|基于Springboot的机器人门户展示系统设计与实现(源码+数据库+文档)
java·数据库·spring boot·机器人·论文·毕设·机器人门户展示系统
notion20251 小时前
Adobe Lightroom Classic下载与安装教程(附安装包) 2025最新版详细图文安装教程
java·数据库·其他·adobe
rengang662 小时前
351-Spring AI Alibaba Dashscope 多模型示例
java·人工智能·spring·多模态·spring ai·ai应用编程
小蒜学长2 小时前
springboot酒店客房管理系统设计与实现(代码+数据库+LW)
java·数据库·spring boot·后端
lang201509282 小时前
Spring MVC配置全解析
java·spring·mvc
せいしゅん青春之我3 小时前
【JavaEE初阶】TCP核心机制10——异常情况的处理
java·网络·笔记·网络协议·tcp/ip·java-ee
摇滚侠3 小时前
Spring Boot3零基础教程,把 Java 程序打包为 Linux 可执行文件,笔记91
java·linux·笔记
mount_myj4 小时前
敏感信息屏蔽(一)【java】
java·算法·极课堂