美文网首页java
java date时间操作

java date时间操作

作者: 指下光年 | 来源:发表于2022-03-15 16:16 被阅读0次

    计算时间差

    public static int secondsBetween(Date beforeDate,Date nowDate) throws Exception  {
            //时间格式
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            beforeDate=sdf.parse(sdf.format(beforeDate));
            nowDate=sdf.parse(sdf.format(nowDate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(beforeDate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(nowDate);
            long time2 = cal.getTimeInMillis();
            return Integer.parseInt(String.valueOf((time2-time1)/(1000)));
    }
    

    获取系统前一天

    public static Date getNextDay(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            date = calendar.getTime();
            return date;
    }
    

    获取当前系统时间

    public static Date getNextDay(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, 0);
            date = calendar.getTime();
            return date;
    }
    

    获取当前系统下一天时间

    public static Date getNextDay(Date date) {
           Calendar calendar = Calendar.getInstance();
           calendar.setTime(date);
           calendar.add(Calendar.DAY_OF_MONTH, -1);
           date = calendar.getTime();
           return date;
    } 
    

    判断一个时间是否在一个时间段内

        /**
         * Description: 判断一个时间是否在一个时间段内 </br>
         *
         * @param nowTime 当前时间 </br>
         * @param beginTime 开始时间 </br>
         * @param endTime 结束时间 </br>
         */
        private boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
            Calendar date = Calendar.getInstance();
            date.setTime(nowTime);
     
            Calendar begin = Calendar.getInstance();
            begin.setTime(beginTime);
     
            Calendar end = Calendar.getInstance();
            end.setTime(endTime);
     
            return date.after(begin) && date.before(end);
        }
    }
    

    相关文章

      网友评论

        本文标题:java date时间操作

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