5 changed files with 128 additions and 5 deletions
@ -0,0 +1,51 @@ |
|||||
|
package com.elink.esua.epdc.commons.tools.utils; |
||||
|
|
||||
|
|
||||
|
import java.time.*; |
||||
|
import java.util.Date; |
||||
|
|
||||
|
/** |
||||
|
* @author work@yujt.net.cn |
||||
|
* @date 2019/12/13 15:05 |
||||
|
*/ |
||||
|
public class LocalDateUtils { |
||||
|
|
||||
|
|
||||
|
public static LocalDateTime dateToLocalDateTime(Date date) { |
||||
|
Instant instant = date.toInstant(); |
||||
|
ZoneId zone = ZoneId.systemDefault(); |
||||
|
return LocalDateTime.ofInstant(instant, zone); |
||||
|
} |
||||
|
|
||||
|
public static LocalDate dateToLocalDate(Date date) { |
||||
|
Instant instant = date.toInstant(); |
||||
|
ZoneId zone = ZoneId.systemDefault(); |
||||
|
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone); |
||||
|
return localDateTime.toLocalDate(); |
||||
|
} |
||||
|
|
||||
|
public static YearMonth dateToYearMonth(Date date) { |
||||
|
LocalDate localDate = dateToLocalDate(date); |
||||
|
return YearMonth.of(localDate.getYear(), localDate.getMonthValue()); |
||||
|
} |
||||
|
|
||||
|
public static LocalTime dateToLocalTime(Date date) { |
||||
|
Instant instant = date.toInstant(); |
||||
|
ZoneId zone = ZoneId.systemDefault(); |
||||
|
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone); |
||||
|
return localDateTime.toLocalTime(); |
||||
|
} |
||||
|
|
||||
|
public static Date localDateTimeToDate(LocalDateTime localDateTime) { |
||||
|
ZoneId zone = ZoneId.systemDefault(); |
||||
|
Instant instant = localDateTime.atZone(zone).toInstant(); |
||||
|
return Date.from(instant); |
||||
|
} |
||||
|
|
||||
|
public static Date localDateToDate(LocalDate localDate) { |
||||
|
ZoneId zone = ZoneId.systemDefault(); |
||||
|
Instant instant = localDate.atStartOfDay().atZone(zone).toInstant(); |
||||
|
return Date.from(instant); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue