我是靠谱客的博主 激动哑铃,最近开发中收集的这篇文章主要介绍时间工具类DateUtils,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

 public static final String DATE_TIME_MS_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT = "yyyy-MM-dd";

    public static final String X_DATE_TIME_MS_FORMAT = "yyyyMMdd HH:mm:ss.SSS";
    public static final String X_DATE_TIME_FORMAT = "yyyyMMdd HH:mm:ss";
    public static final String X_DATE_FORMAT = "yyyyMMdd";

    public static final String TIME_FORMAT = "HH:mm:ss";

    public static final String ONE_MONTH = "30";
    public static final String ONE_WEEKS = "8";
    public static final String TWO_WEEKS = "15";
    public static final String THREE_WEEKS = "22";

    /**
     * 校验是否过期
     *
     * @param date 过期时间
     * @return
     */
    public static boolean isExpire(Date date) {
        return new Date().getTime() > date.getTime();
    }

    public static Date now(){
        return new Date();
    }

    /**
     * 是否在指定时间内
     * @param start_time 开始时间
     * @param end_time   结束时间
     * @return
     * @throws ParseException
     */
    public static boolean isInterval(String start_time,String end_time) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateUtils.TIME_FORMAT);
        Date start_hour_parse = simpleDateFormat.parse(start_time);
        Date end_hour_parse = simpleDateFormat.parse(end_time);
        Date nowTime = simpleDateFormat.parse(simpleDateFormat.format(DateUtils.now()));
        return DateUtil.isIn(nowTime,start_hour_parse,end_hour_parse);
    }


    public static Date parseDateMaxTime(Date expirationTime) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(expirationTime);
        calendar.set(Calendar.HOUR,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);

        return  calendar.getTime();
    }

    /**
     * 昨天 pattern yyyy-mm-dd
     * @return
     */
    public static String lastDay(){
        Date as = new Date(new Date().getTime()-24*60*60*1000);
        System.out.println(as);
        SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
        return matter.format(as);
    }
    public static String lastmonth(){
        Date date = new Date();
//        2、获取本月的年月

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String crrmonth = sdf.format(date);
//        3、获取上个月的此刻

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上1个月
        date = calendar.getTime();
        String month1 = sdf.format(date);
        return month1;
    }


    /**
     * 获取当前时间
     * @return
     */
    public static Date nowTime(){
        long startTime = System.currentTimeMillis();
        Timestamp now1 = new Timestamp(startTime);
        return now1;
    }

    public static Integer nowYear(){
        Date date = nowTime();
        SimpleDateFormat matter = new SimpleDateFormat("yyyy");
        String format = matter.format(date);
        return Integer.valueOf(format);
    }

    public static String nowYearMoth(){
        Date date = nowTime();
        SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM");
        String format = matter.format(date);
        return format;
    }

    /**
     * 当前时间距离过期时间还有多久
     * @param date
     * @return
     */
    public static String difference(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateUtils.DATE_FORMAT);
        String time2 = simpleDateFormat.format(date);
        Date startTime = new Date();
        String time1 = simpleDateFormat.format(startTime);

        Long day = null;
        Date date1 = null;
        Date date2 = null;

        try {
            date1 = simpleDateFormat.parse(time1);
            date2 = simpleDateFormat.parse(time2);
            Calendar startCalendar = Calendar.getInstance();
            startCalendar.setTime(date1);
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(date2);
            Long endSecond = endCalendar.getTimeInMillis();
            Long startSecond = startCalendar.getTimeInMillis();
            Long balance = endSecond-startSecond;
            Integer s_day = 60*60*24*1000;
//            Integer s_hour = 60*60*1000;
//            Integer s_min = 60*1000;
            day = balance/s_day;//还剩多少天
//            Long hour = (balance-day*s_day)/s_hour;//还剩多少小时
//            Long min = (balance-day*s_day-hour*s_hour)/s_min;//还剩多少分钟
//            Long sec = (balance-day*s_day-hour*s_hour-min*s_min)/1000;//还剩多少秒

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return String.valueOf(day);
    }

最后

以上就是激动哑铃为你收集整理的时间工具类DateUtils的全部内容,希望文章能够帮你解决时间工具类DateUtils所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部