JAVA 获取今天,7天前,一个月前,今年起始日期,同比日期 各种时间段的方法归结

本文汇总了Java中获取当前日期、7天前、一个月前以及今年起始日期的方法,同时介绍了如何获取同比日期,为日常开发提供便利。
摘要由CSDN通过智能技术生成
public class DateUtil {
    /**
    *@Author ht
    *@Date 2020/12/2 15:31
     * 当前时间前多少天
    */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    /**
    *@Author ht
    *@Date 2020/12/2 15:29
     * 今天格式 年月日
    */
    public static String getSystemTime(){
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
    }

    /**
    *@Author ht
    *@Date 2020/12/2 15:27
     * 今年一月一号
    */

    public static String getThisYear() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date time = new Date();
        try {
            Calendar currCal=Calendar.getInstance();
            Calendar calendar = Calendar.getInstance();
            calendar.clear();
            calendar.set(Calendar.YEAR,currCal.get(Calendar.YEAR));
            time = calendar.getTime();
        }catch (Exception e){
            e.printStackTrace();
        }
        return format.format(time);
    }

    /**
    *@Author ht
    *@Date 2020/12/2 15:26
     * 去年第一天 一月一号
    */
    public static String getUpYear() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date time = new Date();
        try {
            Calendar currCal=Calendar.getInstance();
            Calendar calendar = Calendar.getInstance();
            calendar.clear();
            calendar.set(Calendar.YEAR,currCal.get(Calendar.YEAR)-1);
            time = calendar.getTime();
        }catch (Exception e){
            e.printStackTrace();
        }
        return format.format(time);
    }

    /**
    *@Author ht
    *@Date 2020/12/2 15:25
     * 一年前的今天开始日期
    */
    public static String getOneYearTimeNow(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, -1); //年份减1
        String start =format.format(c.getTime())+" 00:00:00";
        return start;
    }
    /**
    *@Author ht
    *@Date 2020/12/2 15:25
     * 一年前的今天结束日期
    */
    public static String getOneYearTimeNowOut(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, -1); //年份减1
        String start =format.format(c.getTime())+" 23:59:59";
        return start;
    }

    /**
    *@Author ht
    *@Date 2020/12/2 15:25
     * 今年是哪年
    */
    public static String getSysYear() {
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        return year;
    }
    /**
    *@Author ht
    *@Date 2020/12/2 15:24
     * 去年是哪年
    */
    public static String getUpYearDate() {
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR)-1);
        return year;
    }

    /**
    *@Author ht
    *@Date 2020/12/2 15:24
     * 今天24小时数组
    */

    public static List get24h(){
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ArrayList<String> dates = new ArrayList<String>();
        String s = df.format(day);
        try {
            Date date = df.parse(s);
            for (int i = 0; i < 24; i++) {
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                if (i==0){
                    cal.add(Calendar.HOUR, 0);
                }else{
                    cal.add(Calendar.HOUR, 1);
                }
                date = cal.getTime();
                String s1 = format.format(date);
                dates.add(s1);
            }
            System.out.println(dates.size());
            System.out.println(dates);
        }catch (Exception e){

        }
        return dates;
    }
    /**
    *@Author ht
    *@Date 2020/12/2 11:40
     * 去年今天24小时数组
    */
    public static List getyear24h(){
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ArrayList<String> dates = new ArrayList<String>();
        String s = df.format(day);
        try {
            Date date = df.parse(s);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.YEAR,-1);
            for (int i = 0; i < 24; i++) {
                if (i==0){
                    cal.add(Calendar.HOUR, 0);
                }else{
                    cal.add(Calendar.HOUR, 1);
                }
                date = cal.getTime();
                String s1 = format.format(date);
                dates.add(s1);
            }
            System.out.println(dates.size());
            System.out.println(dates);
        }catch (Exception e){

        }
        return dates;
    }

    /**
    *@Author ht
    *@Date 2020/12/2 11:33
     * 指定年月的最后一天
    */
    public String getLastDay(int year,int month){
        SimpleDateFormat dateFormatYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, 0); //关键!
        return dateFormatYYYYMMDD.format(cal.getTime());
    }
    /**
    *@Author ht
    *@Date 2020/12/2 11:33
     * 指定年月每一天的日期
    */
    public List<String> getMonthFullDay(int year , int month){
        SimpleDateFormat dateFormatYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
        List<String> fullDayList = new ArrayList<>(32);
        // 获得当前日期对象
        Calendar cal = Calendar.getInstance();
        cal.clear();// 清除信息
        cal.set(Calendar.YEAR, year);
        // 1月从0开始
        cal.set(Calendar.MONTH, month-1 );
        // 当月1号
        cal.set(Calendar.DAY_OF_MONTH,1);
        int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int j = 1; j <= count ; j++) {
            fullDayList.add(dateFormatYYYYMMDD.format(cal.getTime()));
            cal.add(Calendar.DAY_OF_MONTH,1);
        }
        return fullDayList;
    }

    /**
    *@Author ht
    *@Date 2020/12/2 14:23
     * 一年前现在时间点的前多少天的日期
    */
    public static String getupYearNowDayDowen(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - year);
        calendar.add(Calendar.YEAR,-1);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
         return  format.format(today);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值