zoukankan      html  css  js  c++  java
  • TimeUtil 工具类

    /**
     * TODO
     *
     * @auther xh
     * @date 6/11/19 3:32 PM
     */
    public class TimeUtil {
    
        public static final String defaultZone = "Asia/Shanghai";
        private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
    
        // 获得某天最大时间 2017-10-15 23:59:59
        public static Date getEndOfDay(Date date) {
            LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.of(defaultZone));
            LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
            return Date.from(endOfDay.atZone(ZoneId.of(defaultZone)).toInstant());
        }
    
        // 获得某天最小时间 2017-10-15 00:00:00
        public static Date getStartOfDay(Date date) {
            LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.of(defaultZone));
            LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
            return Date.from(startOfDay.atZone(ZoneId.of(defaultZone)).toInstant());
    
        }
    
        // 获得某天最大时间 2017-10-15 23:59:59
        public static Instant getEndOfDay(Instant date) {
            LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(Date.from(date).getTime()), ZoneId.of(defaultZone));
            LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
            Date from = Date.from(endOfDay.atZone(ZoneId.of(defaultZone)).toInstant());
            return from.toInstant();
        }
    
    
        // 获得某天最小时间 2017-10-15 00:00:00
        public static Instant getStartOfDay(Instant date) {
            LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(Date.from(date).getTime()), ZoneId.of(defaultZone));
            LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
            Date from = Date.from(startOfDay.atZone(ZoneId.of(defaultZone)).toInstant());
            return from.toInstant();
    
        }
    
        public static String date2Str(Date date) {
            if (date == null) {
                return null;
            }
            ZoneId zone = ZoneId.of(defaultZone);
            LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), zone);
            return dateTimeFormatter.format(localDateTime);
        }
    
        public static String instant2Str(Instant date) {
            ZoneId zone = ZoneId.of(defaultZone);
            LocalDateTime localDateTime = LocalDateTime.ofInstant(date, zone);
            return dateTimeFormatter.format(localDateTime);
        }
    
        public static Date str2Date(String date) {
            if (StringUtils.isBlank(date)) {
                return null;
            }
            date = date.trim();
            if (date.length() == 10) {
                date += " 00:00:00";
            }
            LocalDateTime localDate = LocalDateTime.parse(date, dateTimeFormatter);
            ZoneId zone = ZoneId.of(defaultZone);
            Instant instant = localDate.atZone(zone).toInstant();
            return Date.from(instant);
        }
    
        public static Instant str2Instant(String date) {
            if (StringUtils.isBlank(date)) {
                return null;
            }
            date = date.trim();
            if (date.length() == 10) {
                date += " 00:00:00";
            }
            LocalDateTime localDateTime = LocalDateTime.parse(date, dateTimeFormatter);
            ZoneId zone = ZoneId.of(defaultZone);
            return localDateTime.atZone(zone).toInstant();
        }
    
    
        /**
         * 得到几天前的时间
         *
         * @param d
         * @param day
         * @return
         */
        public static Date getDateBefore(Date d, int day) {
            Calendar now = Calendar.getInstance();
            now.setTime(d);
            now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
            return now.getTime();
        }
    
        /**
         * 得到几天后的时间
         *
         * @param d
         * @param day
         * @return
         */
        public static Date getDateAfter(Date d, int day) {
            Calendar now = Calendar.getInstance();
            now.setTime(d);
            now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
            return now.getTime();
        }
    }
    
    
  • 相关阅读:
    java学习 接口与继承11 默认方法
    java学习 接口与继承10 内部类
    java学习 接口与继承9 抽象类
    java学习 接口与继承8 final
    理解管理信息系统
    vue中的错误日志
    vue中的ref属性
    2.有24颗外观完全一样的小球,其中有一个是空心的,现在只有一个天平,最少称几次能找出这个特殊的球?
    1.有888瓶编了号码的水及10只健康的小白鼠,其中一瓶水有毒,小白鼠饮用毒水一天后会死,最少需要几天可以找到哪瓶水有毒?
    SQL题1两表联查
  • 原文地址:https://www.cnblogs.com/lanqie/p/11542175.html
Copyright © 2011-2022 走看看