Agregue un día hacia atrás a la fecha en Java, o reste un día hacia adelante

Agregue un día hacia adelante o reste un día de la fecha

El siguiente método toma la cadena de aaaa-mm-dd como parámetro de entrada, agrega un día a la hora y devuelve la cadena de aaaa-mm-dd:

	/**
     * 给时间加一天  接受时间类型:yyyy-mm-dd的字符串
     *
     * @return
     */
    public static String addOneDay(String time) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
        //将接收的time中的年月日截取成String数组
        String[] timeStr = time.split("-");
        //确定time中的那一年的那个月份有多少天
        Calendar calendar = Calendar.getInstance();
        int year = Integer.valueOf(timeStr[0]);
        int month = Integer.valueOf(timeStr[1]);
        int day = Integer.valueOf(timeStr[2]);
        calendar.set(year, month, 0);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        //判断time中的日期是否超过了该年该月的最后一天,如果超过就进行以下处理
        if (day >= dayOfMonth) {
            String date = null;
            //判断月份是否是12月,不是就往后加一个月;是的话就把年份加一年
            if (month < 12) {
                month++;
                Date parse = dateFormat.parse(year + "-" + month + "-01");
                date = dateFormat.format(parse);
            } else if (month == 12) {
                year++;
                date = year + "-01-01";
            }
            return date;
        }
        //time中的日期没有超过该年该月的最后一天,则天数往后加一天
        calendar.setTime(dateFormat.parse(time));
        calendar.add(calendar.DATE, 1);
        return dateFormat.format(calendar.getTime());
    }

El siguiente método toma la cadena de aaaa-mm-dd como entrada, resta un día de la hora y devuelve la cadena de aaaa-mm-dd:

	 /**
     * 给时间减一天   接受时间类型:yyyy-mm-dd的字符串
     *
     * @return
     */
    public static String minusOneDay(String time) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
        //将接收的time中的年月日截取成String数组
        String[] timeStr = time.split("-");
        Calendar calendar = Calendar.getInstance();
        int year = Integer.valueOf(timeStr[0]);
        int month = Integer.valueOf(timeStr[1]);
        int day = Integer.valueOf(timeStr[2]);
        //判断time中的日期是否是该年该月的一号,如果不是就往前减一天;如果是就看情况减月份和年份
        if (day <= 1) {
            String date = null;
            //如果月份不是1月,就对月份减一;如果月份是1月,就对年份减一;
            if (month > 1) {
                month--;
                Calendar c = Calendar.getInstance();
                c.set(year, month, 0);
                Date parse = dateFormat.parse(year + "-" + month + "-" + c.get(c.DAY_OF_MONTH));
                date = dateFormat.format(parse);
            } else if (month == 1) {
                year--;
                date = year + "-12-31";
            }
            return date;
        }
        //time中的日期不是该年该月的一号,直接往前减一天
        Date date = dateFormat.parse(time);
        calendar.setTime(date);
        calendar.add(calendar.DATE, -1);
        return dateFormat.format(calendar.getTime());
    }

Los resultados de la prueba son los siguientes:

Ingrese una hora, agregue un día y reste un día a esta hora:

public static void main(String[] args) throws ParseException{
        String s = addOneDay("2021-01-01");
        String s1 = minusOneDay("2021-01-01");
        System.out.println( "加一天:"+s + '\n' + "减一天:"+s1);
}

inserte la descripción de la imagen aquí

Supongo que te gusta

Origin blog.csdn.net/weixin_55118477/article/details/120986508
Recomendado
Clasificación