时间操作工具类

public class DateUtils {

    /**
     * 获取当前时间下个整点时间
     * @return 时间对象
     */
    public static Date getNextWholePoint(){
        return new Date(System.currentTimeMillis()/(1000*60*60)*(1000*60*60)+3600000);
    }

    /**
     * 获取今天零点时间
     * @return 时间对象
     */
    public static Date getTodayZeroTime(){
        long zero = System.currentTimeMillis()/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();
        return new Date(zero);
    }

    /**
     * 获取某天的零点时间
     * @return 时间对象
     */
    public static Date getZeroTime(Date time){
        long zero = time.getTime()/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();
        return new Date(zero);
    }

    /**
     * 获取某天的零点时间
     * @return 时间对象
     */
    public static Date getZeroTime(Long time){
        long zero = time/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();
        return new Date(zero);
    }

    /**
     * 获取未来时间
     * @param day 天数
     * @return 时间对象
     */
    public static Date getFutureDate(int day){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, Math.abs(day));
        return calendar.getTime();
    }

    /**
     * 获取未来时间
     * @param date 需要计算的时间
     * @param day 天数
     * @return 时间对象
     */
    public static Date getFutureDate(Date date,int day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, Math.abs(day));
        return calendar.getTime();
    }


    /**
     * 获取未来时间
     * @param date 需要操作的时间
     * @param millisecond 毫秒数
     * @return 时间对象
     */
    public static Date getFutureDate(Date date,Long millisecond){
        return new Date(date.getTime()+millisecond);
    }

    /**
     * 获取以前时间
     * @param date 需要操作的时间
     * @param millisecond 毫秒数
     * @return 时间对象
     */
    public static Date getBeforeDate(Date date,Long millisecond){
        return new Date(date.getTime()-millisecond);
    }

    /**
     * 获取以前时间
     * @param date 需要操作的时间
     * @param millisecond 毫秒数
     * @return 时间对象
     */
    public static Date getBeforeDate(Date date,Integer millisecond){
        return new Date(date.getTime()-millisecond);
    }

    /**
     * 获取以前时间
     * @param day 天数
     * @return 时间对象
     */
    public static Date getBeforeDate(int day){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -Math.abs(day));
        return calendar.getTime();
    }

    /**
     * 格式化时间
     * @param pattern 时间格式
     * @return 时间格式后字符串
     */
    public static String formatDate(Date time,String pattern){
        if (time != null){
            SimpleDateFormat dateFormater = new SimpleDateFormat(pattern);
            return dateFormater.format(time);
        }
        return null;
    }

    /**
     * 格式化时间
     * @return 时间格式后字符串
     */
    public static String formatDate(Date time){
        if (time != null){
            SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return dateFormater.format(time);
        }
        return null;
    }
}

猜你喜欢

转载自blog.csdn.net/io277800/article/details/80484094