TimestampUtil.java 1.24 KB
package com.topdraw.util;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

public class TimestampUtil {

    public static Timestamp now(){
        return new Timestamp(System.currentTimeMillis());
    }

    public static Timestamp now(LocalDateTime localDateTime) {
        long epochSecond = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return new Timestamp(epochSecond);
    }

    public static long localDateTime2long(LocalDateTime localDateTime){
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static Timestamp localDateTime2Timestamp(LocalDateTime localDateTime){
        long epochSecond = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return long2Timestamp(epochSecond);
    }
    public static Timestamp long2Timestamp(long timestamp){
        return Timestamp.from(Instant.ofEpochMilli(timestamp));
    }

    public static long timestamp2long(Timestamp timestamp){
        return timestamp.toInstant().toEpochMilli();
    }

    public static LocalDateTime long2LocalDateTime(Long expireTime) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(expireTime),ZoneOffset.of("+8"));
    }

}