• 对时间TimeUtils操作总结(1)


    自己对时间操作的时候一些方法

    public static final SimpleDateFormat DATETIME_FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        public static final SimpleDateFormat DATE_FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd");
        public static final SimpleDateFormat DATE_FORMAT_HOURS = new SimpleDateFormat("HH:mm");
        public static final SimpleDateFormat DATE_FORMAT_HOURS_MINUTES_SECOND = new SimpleDateFormat("HH:mm:ss");
    
        public String getToday23() {
            return getToday23(new Date());
        }
        public String getToday23(Date date) {
            Date time = getDesignatedTimeOfDay(date, 23, 59, 59, 59);
            String time23 = formatDateByPattern(time, "yyyy-MM-dd HH:mm:ss");
            return time23;
        }
    
        public String getToday0(){
            return getToday0(new Date());
        }
        public String getToday0(Date date){
            Date time = getDesignatedTimeOfDay(date, 0, 0, 0, 0);
            String time0 = formatDateByPattern(time, "yyyy-MM-dd HH:mm:ss");
            return time0;
        }
        public Date getDesignatedTimeOfDay(Date date,Integer hour,Integer minute,Integer second,Integer millisecond){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.HOUR_OF_DAY, hour);
            calendar.set(Calendar.MINUTE, minute);
            calendar.set(Calendar.SECOND, second);
            calendar.set(Calendar.MILLISECOND, millisecond);
            Date time = calendar.getTime();
            log.info("getDesignatedTimeOfDay--->"+time);
            return time;
        }
    
        /**
         * @param date
         * @param dateFormat
         * @return
         */
        public  String formatDateByPattern(Date date, String dateFormat) {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            String formatTimeStr = null;
            if (date != null) {
                formatTimeStr = sdf.format(date);
            }
            return formatTimeStr;
        }
    
        public Date getBeforeDay(Date date){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, -1); //得到回前一天答
            return calendar.getTime();
        }
        public Date getAfterDay(Date date){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            //得到回后一天答
            calendar.add(Calendar.DATE, +1);
            return calendar.getTime();
        }
    
        public String getAfterDayStr(Date date, String dateFormat){
            SimpleDateFormat format = new SimpleDateFormat(dateFormat);
            Date afterDay = getAfterDay(date);
            return format.format(afterDay);
        }
        public String  getBeforeDay(Date date,DateFormat df){
            return df.format(getBeforeDay(date));
        }
    
        public String getCronDate(String date){
            String[] split = date.split(":");
            String str = split[2]+ " "+split[1]+" "+split[0]+" * * ?";
            return str;
        }
    
        /**
         * 判断当前小时 是否在某两个时间之间
         * @param start_time
         * @param end_time
         * @return
         */
        public boolean isBetweenHours(String start_time,String end_time){
            boolean flag = false;
            try {
                String format = "HH:mm";
                Calendar now = Calendar.getInstance();
                int hours = now.get(Calendar.HOUR_OF_DAY);
                int minute  = now.get(Calendar.MINUTE);
                int second  = now.get(Calendar.SECOND);
                String now_time = hours+":"+minute+":"+second;
                Date nowTime = new SimpleDateFormat(format).parse(now_time);
                Date startTime = new SimpleDateFormat(format).parse(start_time);
                Date endTime = new SimpleDateFormat(format).parse(end_time);
                flag = isEffectiveDate(nowTime, startTime, endTime);
            }catch (Exception e){
    
            }
            return flag;
        }
    
        /**
         * 判断当前小时 是否在某两个时间之间
         * @param start_time
         * @param end_time
         * @return
         */
        public boolean isBetweenDate(String start_time,String end_time,String dateForamte){
            boolean flag = false;
            try {
                SimpleDateFormat df = new SimpleDateFormat(dateForamte);
                String format = df.format(new Date());
                Date nowTime = df.parse(format);
                Date startTime = new SimpleDateFormat(format).parse(start_time);
                Date endTime = new SimpleDateFormat(format).parse(end_time);
                flag = isEffectiveDate(nowTime, startTime, endTime);
            }catch (Exception e){
    
            }
            return flag;
        }
        /**
         * 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
         *
         * @param nowTime 当前时间
         * @param startTime 开始时间
         * @param endTime 结束时间
         * @return
         * @author jqlin
         */
        public  boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
            if (nowTime.getTime() == startTime.getTime()
                    || nowTime.getTime() == endTime.getTime()) {
                return true;
            }
    
            Calendar date = Calendar.getInstance();
            date.setTime(nowTime);
    
            Calendar begin = Calendar.getInstance();
            begin.setTime(startTime);
    
            Calendar end = Calendar.getInstance();
            end.setTime(endTime);
    
            if (date.after(begin) && date.before(end)) {
                return true;
            } else {
                return false;
            }
        }
    
        /**
         * beforeDate 在 afterDate 之前
         * @param beforeDate
         * @param afterDate
         * @return
         */
        public boolean compareHours(String beforeDate,String afterDate){
            boolean flag = false;
            try {
                SimpleDateFormat dateFormat=new SimpleDateFormat("HH:mm:ss");
                Date date=dateFormat.parse(beforeDate);
                Date date2=dateFormat.parse(afterDate);
                flag = date2.after(date);
            }catch (Exception e){
    
            }
            return flag;
        }
    
        /**
         * 获取当前时间时分秒
         * @return
         */
        public String getCurrentHMS(){
            String format ="";
            try {
                SimpleDateFormat df = LilyTimeUtils.DATE_FORMAT_HOURS_MINUTES_SECOND;
                format = df.format(new Date());
            }catch (Exception e){
    
            }
            return format;
        }
    
        /**
         * 获取某个时间  多少分钟之后的时间
         * @param oldTime
         * @param add
         * @return
         * @throws Exception
         */
        public  String  getMinTimeAfter (String oldTime,String add){
            String returnTime = "";
            try {
                int addMit = Integer.valueOf(add);
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = df.parse(oldTime);
                Date expireTime = new Date(date.getTime() + addMit*60*1000);
                returnTime = df.format(expireTime);
            }catch (Exception e){
                e.printStackTrace();
            }
    
            return returnTime;
        }
    

      

  • 相关阅读:
    PHP chop() 函数
    PHP bin2hex() 函数
    多个表关联或者有视图套视图,快速检查SQL语句中所有的表统计信息是否过期
    洛谷P3628 [APIO2010]特别行动队 斜率优化
    洛谷P3195 [HNOI2008]玩具装箱TOY 斜率优化
    bzoj4282 慎二的随机数列 树状数组求LIS + 构造
    校园网 入读统计 + 性质分析
    HAOI2006 受欢迎的牛 缩点
    普通平衡树 Treap
    洛谷P1563 玩具谜题 简单模拟
  • 原文地址:https://www.cnblogs.com/mfser/p/13255912.html
Copyright © 2020-2023  润新知