Java 8 time conversion (localdatetime) code example
•
Java
This article mainly introduces the java8 time conversion (localdatetime) code example. The example code is introduced in great detail, which has a certain reference value for everyone's study or work. Friends in need can refer to it
1. Convert localdatetime to a custom time format string
public static String getDateTimeAsString(LocalDateTime localDateTime,String format) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); return localDateTime.format(formatter); }
2. Convert timestamp of long type to localdatetime
public static LocalDateTime getDateTimeOfTimestamp(long timestamp) { Instant instant = Instant.ofEpochMilli(timestamp); ZoneId zone = ZoneId.systemDefault(); return LocalDateTime.ofInstant(instant,zone); }
3. Convert localdatetime to timestamp of long type
public static long getTimestampOfDateTime(LocalDateTime localDateTime) { ZoneId zone = ZoneId.systemDefault(); Instant instant = localDateTime.atZone(zone).toInstant(); return instant.toEpochMilli(); }
4. Convert a time string to localdatetime in user-defined time format
public static LocalDateTime parseStringToDateTime(String time,String format) { DateTimeFormatter df = DateTimeFormatter.ofPattern(format); return LocalDateTime.parse(time,df); }
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码