美文网首页
日期工具

日期工具

作者: 艾邦斯 | 来源:发表于2019-12-16 16:25 被阅读0次
    public final class DateTimeUtils {
        public static final String PATTERN = "yyyy-MM-dd HH:mm:ss:SSS";
        
        // 日期转Str
        public static String date2Str(Date date) {
            Instant instant = date.toInstant();
            ZoneId zone = ZoneId.systemDefault();
            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
            DateTimeFormatter format = DateTimeFormatter.ofPattern(PATTERN);
            String nowStr = localDateTime.format(format);
            return nowStr;
        }
        // 日期转字符串
        public static Date str2Date(String dateStr) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(PATTERN);
            LocalDateTime localDateTime = LocalDateTime.parse(dateStr, dateTimeFormatter);
            ZoneId zone = ZoneId.systemDefault();
            Instant instant = localDateTime.atZone(zone).toInstant();
            Date date = Date.from(instant);
            return date;
        }
        // 当天日期
        public static String nowTimeStr() {
            return date2Str(new Date());
        }
        // 字符串日期转时间戳
        public static long getTimestamp(String dateStr) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(PATTERN);
            LocalDateTime localDateTime = LocalDateTime.parse(dateStr, dateTimeFormatter);
            ZoneId zone = ZoneId.systemDefault();
            Instant instant = localDateTime.atZone(zone).toInstant();
            return instant.toEpochMilli();
        }
        /**
        * 获取当前毫秒数
        */
        public static Long toEpochMilli(LocalDateTime dateTime) {
            return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        }
        /**
        * 判断是否为闰年
        */
        public static boolean isLeapYear(LocalDate localDate) {
            return localDate.isLeapYear();
        }
    }
    

    相关文章

      网友评论

          本文标题:日期工具

          本文链接:https://www.haomeiwen.com/subject/svsdnctx.html