java常用的时间类转换方法

1,677 阅读2分钟

java8引入了LocalDateTime类,使关于时间的处理更为方便了。 在日常的开发中,与其他后端服务(java、php、python等)、前端服务(js)进行交互的时候,由于时间格式的不统一,使用的时间格式也各种各样,有使用时间戳的(单位为ms或s),有使用字符串的,也有使用Date类的,为了和这些系统进行交互,需要对日期进行一些转换,本文列举一些常见的各种时间格式之间的转换方法。 在使用的时候可以把这些静态方法放在日期处理的util里,供系统的各个module使用。

1. LocalDateTime转换为时间戳(秒)和时间戳(毫秒)

public static Long localDateTimeToMilliseconds(LocalDateTime time) {
    return Timestamp.valueOf(time).getTime();
}
public static Long localDateTimeToSeconds(LocalDateTime time) {
    return localDateTimeToMilliseconds(time) / 1000;
}

2. 时间戳(ms)转换为LocalDateTime

public static LocalDateTime longToLocalDateTime1(Long milliseconds) {
    return new Timestamp(milliseconds).toLocalDateTime();
}
public static LocalDateTime longToLocalDateTime2(Long milliseconds) {
    return LocalDateTime.ofInstant(Instant.ofEpochMilli(milliseconds), ZoneId.systemDefault());
}

3. 字符串日期格式转换为LocalDateTime

public static LocalDateTime dateTimeStrToLocalDateTime(String dateTimeStr, String pattern)
    throws ParseException {
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    long timestamp = format.parse(dateTimeStr).getTime();
    return longToLocalDateTime1(timestamp);
}

4. LocalDateTime转换为字符串日期格式

public static String localDateTimeToDateTimeStr(LocalDateTime localDateTime, String pattern) {
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    return format.format(localDateTimeToDate(localDateTime));
}

5. Date和LocalDateTime互转

public static LocalDateTime dateToLocalDateTime(Date date) {
    Instant instant = date.toInstant();
    return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
}
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
    Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
    return Date.from(instant);
}

6. Timestamp和LocalDateTime互转

public static LocalDateTime timeStampToLocalDateTime(Timestamp timestamp) {
    return timestamp.toLocalDateTime();
}
public static Timestamp localDateTimeToTimeStamp(LocalDateTime localDateTime) {
    return Timestamp.valueOf(localDateTime);
}

7. 获取指定天后的时间戳

public static Timestamp getBeginTimeStampBeforeSomeDays(Timestamp timestamp, long days) {
    LocalDateTime localDateTime = timestamp.toLocalDateTime();
    return Timestamp.valueOf(localDateTime.truncatedTo(ChronoUnit.DAYS).minusDays(days));
}
public static Timestamp getEndTimeStampAfterSomeDays(Timestamp timestamp, long days) {
    LocalDateTime localDateTime = timestamp.toLocalDateTime();
    return Timestamp.valueOf(
        localDateTime.truncatedTo(ChronoUnit.DAYS).plusDays(days + 1).minusNanos(1));
}

测试

public static void main(String[] args) {
    Timestamp now = new Timestamp(1609767109000L);
    LocalDateTime localDateTime = timeStampToLocalDateTime(now);
    // 1609767109
    System.out.println(localDateTimeToSeconds(localDateTime));
    // 1609767109000
    System.out.println(localDateTimeToMilliseconds(localDateTime));
    // 2021-01-02 00:00:00.0
    System.out.println(getBeginTimeStampBeforeSomeDays(now, 2));
    // 2021-01-06 23:59:59.999999999
    System.out.println(getEndTimeStampAfterSomeDays(now, 2));
    // 2021-01-04 21:31:49
    System.out.println(localDateTimeToDateTimeStr(localDateTime, "yyyy-MM-dd HH:mm:ss"));
}