zoukankan      html  css  js  c++  java
  • LocalDateTime

    @Component
    public class DateUtil {
    
        public final static String EMPTY_SRING = "";
        public final static String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
        public final static long DATE_TIME = 1000 * 24 * 60 * 60;
        public final static long HOUR_TIME = 1000 * 60 * 60;
        public final static long MINUTE_TIME = 1000 * 60;
        public LocalDateTime parseDate(String timeStr) {
    //        return new LocalDateTimeStringConverter().fromString(timeStr);
            DateTimeFormatter pattern =
                    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
            //严格按照ISO yyyy-MM-dd验证,03写成3都不行
            return LocalDateTime.parse(timeStr, pattern);
        }
    
        public String currentDateDay() {
            LocalDate ldt = LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            return ldt.format(formatter);
        }
    
        public String currentDateTime() {
            LocalDateTime ldt = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            return ldt.format(formatter);
        }
    
        public String afterCurrentDate(LocalDateTime dt, int mount, int unit) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime ldt = LocalDateTime.now();
            if (dt != null) {
                ldt = dt;
            }
            if (unit == ConstUtil.UNIT_SECOND) {
                return ldt.plusSeconds(mount).format(formatter);
            } else if (unit == ConstUtil.UNIT_MINUTE) {
                return ldt.plusMinutes(mount).format(formatter);
            } else if (unit == ConstUtil.UNIT_HOUR) {
                return ldt.plusHours(mount).format(formatter);
            } else if (unit == ConstUtil.UNIT_DAY) {
                return ldt.plusDays(mount).format(formatter);
            } else if (unit == ConstUtil.UNIT_WEEK) {
                return ldt.plusWeeks(mount).format(formatter);
            } else if (unit == ConstUtil.UNIT_MONTH) {
                return ldt.plusMonths(mount).format(formatter);
            } else if (unit == ConstUtil.UNIT_YEAR) {
                return ldt.plusYears(mount).format(formatter);
            }
            return ldt.format(formatter);
        }
    
        public String beforeCurrentDate(LocalDateTime dt, int mount, int unit) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime ldt = LocalDateTime.now();
            if (dt != null) {
                ldt = dt;
            }
            if (unit == ConstUtil.UNIT_SECOND) {
                return ldt.minusSeconds(mount).format(formatter);
            } else if (unit == ConstUtil.UNIT_MINUTE) {
                return ldt.minusMinutes(mount).format(formatter);
            } else if (unit == ConstUtil.UNIT_HOUR) {
                return ldt.minusHours(mount).format(formatter);
            } else if (unit == ConstUtil.UNIT_DAY) {
                return ldt.minusDays(mount).format(formatter);
            } else if (unit == ConstUtil.UNIT_WEEK) {
                return ldt.minusWeeks(mount).format(formatter);
            } else if (unit == ConstUtil.UNIT_MONTH) {
                return ldt.minusMonths(mount).format(formatter);
            } else if (unit == ConstUtil.UNIT_YEAR) {
                return ldt.minusYears(mount).format(formatter);
            }
            return ldt.format(formatter);
        }
    
        /**
         * @param date
         * @return Date
         */
        public static Date convertStringToDate(String date) {
            try {
                return new SimpleDateFormat(DEFAULT_PATTERN).parse(date);
            } catch (ParseException e) {
                return null;
            }
        }
    
        /**
         * 把日期转换成yyyy-MM-dd HH:mm:ss格式
         *
         * @param date
         * @return String
         */
        public static String convertDate(Date date) {
            if (date == null) {
                return EMPTY_SRING;
            }
            return new SimpleDateFormat(DEFAULT_PATTERN).format(date);
        }
        /**
         * 把时间加上day天后返回,如果传负数代表减day天
         *
         * @param date
         * @param day
         * @return Date
         */
        public static Date dateAdd(Date date, int day) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + day);
            return calendar.getTime();
        }
    }
  • 相关阅读:
    转载:SSH无法连接error:couldnotloadhostkey:/etc/ssh/ssh_host_dsa_key
    docker修改运行中的容器端口映射
    查看iis进程(w3wp)所对应的程序池名称 / 端口使用情况
    jenkins+sonar+钉钉 发布.net
    windows使用jenkins 搭建 .net 自动发布IIS站点平台
    Redis
    20191209---自定义异常类--转载
    借助pywinauto实现本地文件上传--转载
    python虚拟环境搭建,虚拟环境迁移,三方库安装
    python 在不同层级目录import 模块的方法
  • 原文地址:https://www.cnblogs.com/yoyotl/p/11734531.html
Copyright © 2011-2022 走看看