获取指定时间上个20点和下个20点

话不多说

 //time  --->   false前一个时间 true后一个时间
    public static Date changelessTime(boolean time, Date date) {
        try {
            //指定时分秒 注意前面有一个空格
            String times = " 20:00:00";
            Calendar now = Calendar.getInstance();
            now.setTime(date);
            int year = now.get(Calendar.YEAR);
            int months = (now.get(Calendar.MONTH) + 1);
            int day = now.get(Calendar.DAY_OF_MONTH);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date dt2 = df.parse(year + "-" + months + "-" + day + times);
            Date beforeDate = df.parse(year + "-" + months + "-" + (day - 1) + times);
            Date afterDate = df.parse(year + "-" + months + "-" + (day + 1) + times);

            if ((date.getTime() > dt2.getTime()) && time == false) {
                return dt2;
            } else if (date.getTime() < dt2.getTime() && time == false) {
                return beforeDate;
            } else if (date.getTime() > dt2.getTime() && time == true) {
                return afterDate;
            } else if (date.getTime() < dt2.getTime() && time == true) {
                return dt2;
            } else {
                return beforeDate;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

大家可以灵活封装 得意

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页