TimeUtils.java 3.7 KB
package com.tanpu.community.util;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class TimeUtils {
    public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }

    public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    //计算迄今时间
    public static String calUpToNowTime(LocalDateTime start) {
        Duration between = Duration.between(start, LocalDateTime.now());
        long duration = between.toMinutes();
        if (duration < 1) {
            return "刚刚";
        } else if (duration < 60) {
            return duration + "分钟前";
        } else if (duration < 60 * 24) {
            return duration / 60 + "小时前";
        } else if (duration < 60 * 48) {
            return "1天前";
        } else if (duration < 60 * 24 * 180) {
            return start.format(DateTimeFormatter.ofPattern("MM-dd HH:mm"));
        }
        return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
    }

    //格式化时间
    public static String format(LocalDateTime start) {
        return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
    }

    public static LocalDateTime reFormat(String time) {

       return LocalDateTime.parse(time,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
    }

    //计算迄今分钟
    public static long calMinuteTillNow(LocalDateTime start) {
        if (start==null){
            return 0L;
        }
        Duration between = Duration.between(start, LocalDateTime.now());
        return between.toMinutes();
    }

    //计算迄今毫秒数
    public static long calMillisTillNow(LocalDateTime start) {
        if (start==null){
            return 0L;
        }
        Duration between = Duration.between(start, LocalDateTime.now());
        return between.toMillis();
    }

    //计算迄今天数
    public static long calDaysTillNow(LocalDateTime start) {
        if (start==null){
            return 0L;
        }
        Duration between = Duration.between(start, LocalDateTime.now());
        return between.toDays();
    }

    //计算迄今小时数
    public static long calHoursTillNow(LocalDateTime start) {
        if (start==null){
            return 0L;
        }
        Duration between = Duration.between(start, LocalDateTime.now());
        return between.toHours();
    }


    //计算n天前的时间
    public static LocalDateTime getDaysBefore(Integer number) {
        return LocalDateTime.now().minusDays(number);
    }

    public static void main(String[] args) {
        System.out.println(calUpToNowTime(LocalDateTime.now().minusDays(180)));
        System.out.println(calUpToNowTime(LocalDateTime.now().minusDays(1)));
        System.out.println(calUpToNowTime(LocalDateTime.now().minusDays(18)));
        System.out.println(calUpToNowTime(LocalDateTime.now().minusHours(2)));
        System.out.println(calUpToNowTime(LocalDateTime.now().minusMinutes(12)));
        System.out.println(calUpToNowTime(LocalDateTime.now().minusSeconds(12)));
    }

    public static boolean lessThan(String cacheTime, LocalDateTime updateTime) {
        return calMillisTillNow(reFormat(cacheTime)) > calMillisTillNow(updateTime);
    }

    public static boolean lessThan(LocalDateTime before, LocalDateTime after) {
        return calMillisTillNow(before) > calMillisTillNow(after);
    }
}