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
- java.text.ParseException: Unparseable date: “2021-06-24“
- Mutual conversion between date and localdatetime
- Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- Get the current date in Python
- Linux view current date and time
- Ecarts comes with the date formatting method formattime
- ‘Conversion failed when converting date and/or time from character string.DB-Lib error message 241,
- Network agent problem: task: preparekotlinbuildscriptmodel up-to-date
- [JS] use date. Now(). Tostring() to generate serial number
- SAP RETAIL WA01 Creative Distribution List Error – Plant 0000000039 Confirmation date not maintained. –
- [BUG-mybatis-plus] org.postgresql.util.PSQLException: ERROR: syntax error at or near “current_date“
- ERROR 2026 (HY000): SSL connection error: ASN: before date in the future
- Vitis: platform out of date, makefile error at compile time; The modified application compiles to undefined reference
- Stata external commands: the most common and up-to-date commands
- [Solved] Flowable Start Error: ClassCastException: java.time.LocalDateTime cannot be cast to java.lang.String
- Local refresh of listview and GridView
- Error in idea compilation: java.lang.OutOfMemoryError Java heap space and java.lang.StackOverflowError
- java.lang.NoSuchMethodError: org.json.JSONObject.put(Java/lang/String; Java/util/Collection;) “209151;”
- Android:Field can be converted to a local varible.