Java gets the year, month, day before the date, a few days before the specified date, and a few days after

1. Method 1: If it is java8, you can use LocalDate to get it

         // 获取当前日期前一天的日期
         LocalDate localDate = LocalDate.now().minusDays(1);//1代表提前多少天
         // 获取时间字符串如: 2023-03-01
         System.out.println(localDate.toString());
         // 获取当前年如 2023
         System.out.println(localDate.getYear());
         // 获取当前月如 3
         System.out.println(localDate.getMonthValue());
         // 获取当前天如 1
         System.out.println(localDate.getDayOfMonth());
         //获取前一天的日期
         String date = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));//转为String类型

Convert LocalDate to a string in the specified format

method 1

        LocalDate localDate = LocalDate.parse("2022-02-02");
        String date = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        System.out.println(date); //2022-02-02

Method 2

        LocalDate localDate = LocalDate.parse("2022-02-02");
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String date = dtf.format(localDate);
        System.out.println(date); //2022-02-02

2. Method 2: Use the Calendar class

        //获取当前日期
        Date date = new Date();
        //将时间格式化成yyyy-MM-dd HH:mm:ss的格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //创建Calendar实例
        Calendar cal = Calendar.getInstance();
        //设置当前时间
        cal.setTime(date);
        //在当前时间基础上减一年
        cal.add(Calendar.YEAR, -1);  
        System.out.println(format.format(cal.getTime()));
        //在当前时间基础上减一月
        cal.add(Calendar.MONTH,-1);
        System.out.println(format.format(cal.getTime()));
        //同理增加一天的方法:
        cal.add(Calendar.DATE, 1);
        System.out.println(format.format(cal.getTime()));

3. Get the specified time

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//定义日期显示格式
        String now = sdf.format(new Date());//当前日期
        String nowMonthLateDay = getNextDays(now, "-1");//当前日期的前一天
        Calendar datees = Calendar.getInstance();
        String years = String.valueOf(datees.get(Calendar.YEAR)) + "-11-15";//获取当前年的11-15
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//定义日期显示格式
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1);//当前月减去一个月
        String before = sdf.format(c.getTime());//上个月的当前日期
        String now = sdf.format(new Date());//当前日期
        String nowMonthLateDay = getNextDay(now, "-1");//当前日期的前一天
        String lateMonthLateDay = getNextDay(before, "-1");//上个月的当前日期的前一天
        Calendar cal = Calendar.getInstance();
//        int m=cal.get(Calendar.MONTH)+1;//获取当前月份
//        System.err.println("当前月份:"+m);
        String nowMonthFirstDay = getFirstDayOfMonth(1);//获取当前月份的第一天
        cal.add(Calendar.MONTH, -1);
        cal.set(Calendar.DAY_OF_MONTH, 1); //重写当前日期
        String lateMonthFirstDay = sdf.format(cal.getTime());//获取上个月的第一天

4. Get the current year, if it exceeds a certain time, subtract one year or add one year

  Date date = new Date();//获取日期
        SimpleDateFormat dateFormats= new SimpleDateFormat("yyyy");//日期格式
        String year=dateFormats.format(date);//获取当前年度
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //把String转为LocalDate
        LocalDate localTime=LocalDate.parse(year+"-06-30",dtf);
        Integer years=Integer.valueOf(year);
        if (!LocalDate.now().isAfter(localTime)){//判断时间是否超过
            years=Integer.valueOf(year)-1;
        }
        System.err.println(years);
    }

5. Tool class for getting time

 
    /**
     * 将短时间格式字符串转换为时间 yyyy-MM-dd
     *
     * @param strDate
     * @return
     */
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }
 
    /**
     * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
     */
    public static String getNextDay(String nowdate, String delay) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String mdate = "";
            Date d = strToDate(nowdate);
            long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
            d.setTime(myTime * 1000);
            mdate = format.format(d);
            return mdate;
        } catch (Exception e) {
            return "";
        }
    }
 
 
    /**
     * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
     */
    public static String getNextDays(String nowdate, String delay) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("-MM-dd");
            String mdate = "";
            Date d = strToDate(nowdate);
            long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
            d.setTime(myTime * 1000);
            mdate = format.format(d);
            return mdate;
        } catch (Exception e) {
            return "";
        }
    }
 
    /**
     * 获取当前小时
     *
     * @return
     */
    public static String getTimeShort() {
        SimpleDateFormat formatter = new SimpleDateFormat(" HH");
        Date currentTime = new Date();
        String dateString = formatter.format(currentTime);
        return dateString;
    }
 
    /**
     * 获取当前月第一天
     *
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int month) {
        Calendar calendar = Calendar.getInstance();
        // 设置月份
        calendar.set(Calendar.MONTH, month - 1);
        // 获取某月最小天数
        int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最小天数
        calendar.set(Calendar.DAY_OF_MONTH, firstDay);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String firstDays = sdf.format(calendar.getTime());
        return firstDays;
    }
//g过去年度
    public  static String gerLateYear(Integer num){
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        Calendar c = Calendar.getInstance();
        //过去一年
        c.setTime(new Date());
        c.add(Calendar.YEAR, num);
        Date y = c.getTime();
        String year = format.format(y);
        return  year;
 
    }
    //判断是否超过指定时间
    public static boolean afterDate(String date) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
 
        //把String转为LocalDate
        LocalDate localTime = LocalDate.parse(date, dtf);
        //判断当前日期是否大于指定日期
        return LocalDate.now().isAfter(localTime);
    }

Guess you like

Origin blog.csdn.net/qq_43985303/article/details/131576310