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

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);
    }
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页