The most common methods and time conversions of LocalDateTime
After the Java 8 version, LocalDateTime and LocalDate classes have been added, making the conversion convenience no less than jodaTime.
There are many LocalDateTime methods. This article lists some of the most commonly used time conversions in development and gives examples after conversion. I hope you can refer to them when converting.
String to LocalDateTime
public static LocalDateTime parseStringToDateTime(String time, String format) { DateTimeFormatter df = (format); return (time, df); }
LocalDateTime to string
public static String getDateTimeToString(LocalDateTime localDateTime, String format) { DateTimeFormatter formatter = (format); return (formatter); }
test:
If the millisecond bit is not 000, milliseconds will be displayed.
String utc = getUTCMillisecondFromTimestamp(1539718265123); (utc); Output:2018-10-16T19:31:05.123Z
If milliseconds are 000, milliseconds will not be displayed.
String utc = getUTCMillisecondFromTimestamp(1539718265000); (utc); Output:2018-10-16T19:31:05Z
Long-type timestamp (15-bit) to UTC time string
/** * Convert UTC timestamp to UTC string (exactly to seconds) * * @param timestamp * @return */ public static String getUTCStrFromTimestamp(long timestamp) { DateTimeFormatter formatter = ("yyyy-MM-dd'T'HH:mm:ss'Z'"); // Beijing time: ("UTC+8"); system default time: () LocalDateTime localDateTime = ((timestamp), ("UTC")); String utc = (formatter); return utc; }
test:
String utc = getUTCStrFromTimestamp(1539718265123); (utc); Output:2018-10-16T19:31:05Z
Long-type timestamp (15-bit) to UTC time string
/** * Convert the timestamp to a UTC string (if milliseconds are 000, no milliseconds are displayed, and if milliseconds are not 000, no milliseconds are displayed) * * Return format: 2018-10-16T19:31:05.123Z * * @param timestamp * @return */ public static String getUTCMillisecondFromTimestamp(long timestamp) { Instant instant = (timestamp); String utcString = (); return utcString; }
Get the start and end time of today
DateTimeFormatter formatter = ("yyyy-MM-dd'T'HH:mm:ss'Z'"); String startTime = ().withHour(0).withMinute(0).withSecond(0).withNano(0).format(formatter); String endTime = ().withHour(23).withMinute(59).withSecond(59).withNano(999_999_999).format(formatter);
Return to the last day of the target month
/** * Return to the last day of the target month * * @param sourceDate Target month * @param monthOffset Month Offset * @return */ private LocalDate getLastDateOfMonth(LocalDate sourceDate, int monthOffset) { return (monthOffset).with(()); }
Get the start and end time of a day (elegant)
LocalDate date = (2021, 4, 20); LocalDateTime startTime = (date, ); LocalDateTime endTime = (date, );
LocalDateTime Sets Custom Time
/** * Get a ten-minute starting time stamp of a (millisecond) timestamp * * For example: Enter the timestamp of 1534930875000 (2018-08-22 17:41:15) returns the timestamp of 1534930800000 (2018-08-22 17:40:00) * * @param timestrap * * * @return */ public static long getTenMinuteStartTimestrap(long timestamp) { LocalDateTime localDateTime = getLocalDateTimeByTimestamp(timestamp); int minute = (); // The number of single digits in minutes is set to 0 int minute_floor = minute / 10 * 10; LocalDateTime startLocalDateTime = (minute_floor).withSecond(0).withNano(0); long startTimestamp = getTimestampByLocalDateTime(startLocalDateTime); return startTimestamp; }
Long millisecond timestamp to LocalDateTime
/** * Long millisecond timestamp to LocalDateTime */ public static LocalDateTime getLocalDateTimeByTimestamp(long timestamp) { Instant instant = (timestamp); LocalDateTime localDateTime = (instant, ); return localDateTime; }
Get millisecond timestamp based on LocalDateTime
/** * Get LocalDateTime based on millisecond timestamp * @param localDateTime * @return */ public static long getTimestampByLocalDateTime(LocalDateTime localDateTime) { long timestamp = (("UTC")).toInstant().toEpochMilli(); return timestamp; }
Long timestamp is converted to local date according to time zone
/** * Timestamp is converted to local date according to time zone * @param timestamp * @param timezone * @return */ public static Date getLocalDateByTimezone(long timestamp, int timezone) { Instant instant = (timestamp); LocalDateTime localDateTime = (instant, ); // Local time LocalDateTime localDate = (timezone); DateTimeFormatter df = ("yyyy-MM-dd'T'HH:mm:ss'Z'"); String localDateTimeStr = (localDate); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); Date date = null; try { date = (localDateTimeStr); } catch (ParseException e) { (); } return date; }
LocalDateTime to Date
Note: The system time zone
Date date = (().atZone(()).toInstant());
Date to LocalDateTime
Date todayDate = new Date(); LocalDateTime ldt = () .atZone( () ) .toLocalDateTime();
Milliseconds (timestamp)
long second = (1636338645000L).getEpochSecond();
Milliseconds (timestamp)
long second = (1636338645L).toEpochMilli();
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.