Java8 introduces the LocalDateTime class to make time handling easier. In daily development, when interacting with other back-end services (Java, PHP, Python, etc.) and front-end services (JS), due to the differences in time formats, various time formats are used, including timestamp (ms or S), string, and Date class. In order to interact with these systems, dates need to be converted, and this article lists some common conversions between various time formats. You can use these static methods in the date-handling util for use by each module of the system.

1. Convert LocalDateTime to timestamp (s) and timestamp (ms)

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

2. Convert the ms timestamp to 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());
}
Copy the code

3. Convert the string date format to 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);
}
Copy the code

4. Convert LocalDateTime to a string date format

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

5. Transfer between Date and 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);
}
Copy the code

6. Timestamp and LocalDateTime are exchanged

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

7. Obtain the time stamp of the specified day

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));
}
Copy the code

test

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));
    / / the 2021-01-06 23:59:59. 999999999
    System.out.println(getEndTimeStampAfterSomeDays(now, 2));
    / / the 2021-01-04 21:31:49
    System.out.println(localDateTimeToDateTimeStr(localDateTime, "yyyy-MM-dd HH:mm:ss"));
}
Copy the code