94

在 Java 中,如何将一个月添加到当前日期?

4

16 回答 16

125
Calendar cal = Calendar.getInstance(); 
cal.add(Calendar.MONTH, 1);
于 2011-02-05T06:08:50.390 回答
58

爪哇 8

LocalDate futureDate = LocalDate.now().plusMonths(1);
于 2015-03-25T07:59:08.073 回答
33

您可以使用 apache 的 commons lang DateUtils helper 实用程序类。

Date newDate = DateUtils.addMonths(new Date(), 1);

您可以在http://commons.apache.org/proper/commons-lang/下载 commons lang jar

于 2014-03-05T12:57:50.767 回答
32
于 2014-08-01T06:25:33.873 回答
12
Calendar cal = Calendar.getInstance(); 
cal.add(Calendar.MONTH, 1);
java.util.Date dt = cal.getTime();
于 2014-10-29T10:14:40.227 回答
6

(改编自杜古)

public static Date addOneMonth(Date date)
{
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.MONTH, 1);
    return cal.getTime();
}
于 2014-10-02T07:20:32.440 回答
6

您可以使用 org.apache.commons.lang3.time 包中的 DateUtils 类

DateUtils.addMonths(new Date(),1);
于 2019-09-07T11:23:31.817 回答
6

使用压延机并尝试此代码。

Calendar calendar = Calendar.getInstance();         
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
Date nextMonthFirstDay = calendar.getTime();
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Date nextMonthLastDay = calendar.getTime();
于 2016-05-31T05:14:16.787 回答
5
public Date  addMonths(String dateAsString, int nbMonths) throws ParseException {
        String format = "MM/dd/yyyy" ;
        SimpleDateFormat sdf = new SimpleDateFormat(format) ;
        Date dateAsObj = sdf.parse(dateAsString) ;
        Calendar cal = Calendar.getInstance();
        cal.setTime(dateAsObj);
        cal.add(Calendar.MONTH, nbMonths);
        Date dateAsObjAfterAMonth = cal.getTime() ;
    System.out.println(sdf.format(dateAsObjAfterAMonth));
    return dateAsObjAfterAMonth ;
}`
于 2017-04-05T21:39:11.533 回答
2

此方法返回当前日期加上 1 个月。

public Date  addOneMonth()  {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 1);
        return cal.getTime();
}`
于 2017-04-05T22:18:19.393 回答
2

如果您需要一个单行(即 Jasper Reports 公式)并且不介意调整是否不完全是一个月(即“30 天”就足够了):

new Date($F{invoicedate}.getTime() + 30L * 24L * 60L * 60L * 1000L)

于 2016-04-22T11:27:36.983 回答
1

你可以这样使用;

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String d = "2000-01-30";
Date date= new Date(sdf.parse(d).getTime());
date.setMonth(date.getMonth() + 1);
于 2017-07-25T10:39:58.923 回答
1
    Date dateAfterOneMonth = new DateTime(System.currentTimeMillis()).plusMonths(1).toDate();
于 2018-11-21T11:26:33.853 回答
1

为了找到一个月后的哪一天,有必要查看今天是一个月中的哪一天。

因此,如果这一天是每月的第一天,请运行以下代码

Calendar calendar = Calendar.getInstance();

    Calendar calFebruary = Calendar.getInstance();
    calFebruary.set(Calendar.MONTH, Calendar.FEBRUARY);

    if (calendar.get(Calendar.DAY_OF_MONTH) == 1) {// if first day of month
    calendar.add(Calendar.MONTH, 1);
    calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
    Date nextMonthFirstDay = calendar.getTime();
    System.out.println(nextMonthFirstDay);

    }

如果这一天是一个月的最后一天,请运行以下代码。

    else if ((calendar.getActualMaximum(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH))) {// if last day of month
    calendar.add(Calendar.MONTH, 1);
    calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    Date nextMonthLastDay = calendar.getTime();
    System.out.println(nextMonthLastDay);
    }

如果这一天在二月运行以下代码

    else if (calendar.get(Calendar.MONTH) == Calendar.JANUARY
            && calendar.get(Calendar.DAY_OF_MONTH) > calFebruary.getActualMaximum(Calendar.DAY_OF_MONTH)) {// control of february

    calendar.add(Calendar.MONTH, 1);
    calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    Date nextMonthLastDay = calendar.getTime();
    System.out.println(nextMonthLastDay);

    }

以下代码用于其他情况。

    else { // any day
    calendar.add(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    Date theNextDate = calendar.getTime();
    System.out.println(theNextDate);
    }
于 2015-10-08T10:52:02.383 回答
0

常量是葡萄牙语的,因为yes,但是 javadoc 是可以理解的。

打电话

Calendar cal = Calendar.getInstance();
cal.setTime(yourDate);
DateSumUtil.sumOneMonth(cal);

就是这样。相关代码:

package you.project.your_package_utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

public class DateSumUtil {

    private static Integer[] meses31 = { 2, 4, 7, 9 };
    private static List<Integer> meses31List = Arrays.asList(meses31);
    private static SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy");

    private static final int MES = Calendar.MONTH;
    private static final int ANO = Calendar.YEAR;
    private static final int DIA = Calendar.DAY_OF_MONTH;

    /**
     * Receives a date and adds one month. <br />
     * 
     * @param c date to receive an added month, as {@code java.util.Calendar}
     * @param dia day of month of the original month
     */
    public static void addOneMonth(Calendar c, int dia) throws ParseException {
    if (cal.get(MES) == 0) {            if (dia < 29)           cal.add(MES, 1);
        else {  if (cal.get(ANO) % 4 == 0) {    if (dia < 30)   cal.add(MES, 1);
                                                else            cal.setTime(s.parse("29/02/" + cal.get(ANO)));
                } else {                        if (dia < 29)   cal.add(MES, 1);
                                                else            cal.setTime(s.parse("28/02/" + cal.get(ANO)));
    }   }   } else if (meses31List.contains(cal.get(MES))) {
        if (dia < 31) {                                         cal.add(Calendar.MONTH, 1);
                                                                cal.set(DIA, dia);
        } else  cal.setTime(s.parse("30/" + (cal.get(MES) + 2) + "/" + cal.get(ANO)));
    } else {                                                    cal.add(MES, 1);
                                                                cal.set(DIA, dia);  }   
}
于 2021-09-03T12:07:04.550 回答
-3
public class StringSplit {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        date(5, 3);
        date(5, 4);
    }

    public static String date(int month, int week) {
        LocalDate futureDate = LocalDate.now().plusMonths(month).plusWeeks(week);
        String Fudate = futureDate.toString();
        String[] arr = Fudate.split("-", 3);
        String a1 = arr[0];
        String a2 = arr[1];
        String a3 = arr[2];
        String date = a3 + "/" + a2 + "/" + a1;
        System.out.println(date);
        return date;
    }
}

输出:

10/03/2020
17/03/2020
于 2019-09-18T08:11:44.763 回答