Java8 datetime API, add LocalDate, LocalDateTime, LocalTime and other thread-safe classes:
LocalDate: only the date, such as: 2019-07-13LocalTime: only the time, such as: 08:30LocalDateTime: date + time, such as: 2019-07-13 08:30
1. CONVERT DATE TO LOCALDATE
public static LocalDate date2LocalDate(Date date) {
if(null == date) {
return null;
}
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
2. CONVERT LOCALDATE TO DATE
public static Date localDate2Date(LocalDate localDate) {
if(null == localDate) {
return null;
}
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
return Date.from(zonedDateTime.toInstant());
}
2.LocalDateTime is converted to Date
public static Date localDateTime2Date(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
3. LocalDate format
public static String formatDate(Date date) {
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}
Read More:
- Local date time conversion in java8
- Mutual conversion between date and localdatetime
- Elasticsearch in Spring uses Spel to dynamically create Index of Documet class
- Linux view current date and time
- Time, strftime and strptime in Python
- SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
- Abnormal reading after stm32f4 RTC time setting
- Get the current date in Python
- error: expected unqualified-id before ‘.’ token
- Compare whether two sets are the same in Java
- Encapsulation of adding, deleting and modifying database by JDBC
- Common errors and modification methods of findbug
- Realization of springboot authorization verification technology based on JWT
- Usage of NVL in SQL
- Springboot integrates quartz timing tasks
- C ා programming encountered an object reference is required for the non-static field, method, or property error
- Map to vector pair map.second sort
- ‘Conversion failed when converting date and/or time from character string.DB-Lib error message 241,
- In SQLite database, set the default value for the field as the current time
- Shell gets the current time of the system and formats it