Created on 17/4/8 18:55 星期六. @author SeanDragon
| 17 | * @author SeanDragon |
| 18 | */ |
| 19 | public final class ToolDatePlus { |
| 20 | |
| 21 | private ToolDatePlus() { |
| 22 | throw new UnsupportedOperationException("我是工具类,别初始化我。。。"); |
| 23 | } |
| 24 | |
| 25 | private static volatile ZoneId DEFAULT_ZONE_ID = ZoneId.systemDefault(); |
| 26 | |
| 27 | public static LocalDateTime date2LocalDateTime(final Date date) { |
| 28 | Instant instant = date.toInstant(); |
| 29 | ZonedDateTime zdt = instant.atZone(DEFAULT_ZONE_ID); |
| 30 | return zdt.toLocalDateTime(); |
| 31 | } |
| 32 | |
| 33 | public static LocalDateTime time2LocalDateTime(final long time) { |
| 34 | return date2LocalDateTime(new Date(time)); |
| 35 | } |
| 36 | |
| 37 | public static Date localDateTime2Date(final LocalDateTime localDateTime) { |
| 38 | Instant instant = localDateTime.atZone(DEFAULT_ZONE_ID).toInstant(); |
| 39 | return Date.from(instant); |
| 40 | } |
| 41 | |
| 42 | public static long localDateTime2time(final LocalDateTime localDateTime) { |
| 43 | return Timestamp.valueOf(localDateTime).getTime(); |
| 44 | } |
| 45 | |
| 46 | public static String format(final Date date, final String pattern) { |
| 47 | return date2LocalDateTime(date).format(DateTimeFormatter.ofPattern(pattern)); |
| 48 | } |
| 49 | |
| 50 | public static ZoneId getDefaultZoneId() { |
| 51 | return DEFAULT_ZONE_ID; |
| 52 | } |
| 53 | |
| 54 | public static void setDefaultZoneId(ZoneId defaultZoneId) { |
| 55 | DEFAULT_ZONE_ID = defaultZoneId; |
| 56 | } |
| 57 | |
| 58 | private static final Map<String, DateTimeFormatter> DATE_TIME_FORMATTER_MAP = Maps.newConcurrentMap(); |
| 59 | |
| 60 | public static DateTimeFormatter pattern(final String patternStr) { |
| 61 | DateTimeFormatter result = DATE_TIME_FORMATTER_MAP.get(patternStr); |
| 62 | if (result == null) { |
| 63 | result = DateTimeFormatter.ofPattern(patternStr); |
| 64 | DATE_TIME_FORMATTER_MAP.put(patternStr, result); |
| 65 | } |
| 66 | return result; |
| 67 | } |
| 68 | } |
nothing calls this directly
no outgoing calls
no test coverage detected