【问题标题】:Add month with JodaTime?用 JodaTime 添加月份?
【发布时间】:2014-08-14 04:31:44
【问题描述】:

我正在尝试使用 JodaTime 添加月份。我有一个包含几个月的付款计划,例如:一个月、两个月、三个月和六个月。

当我在 DateTime 添加六个月时不起作用并返回异常。

我正在尝试这个。

/** cria data vencimento matricula */    
    public void getDataVencimento(Integer dia, Integer planoPagamento){
        //monta data para JodaTime
        DateTime data = new DateTime();
        Integer ano = data.getYear();
        Integer mes = data.getMonthOfYear() + 6; //here 6 month

        //monta a data usando JodaTime
        DateTime dt = new DateTime(ano, mes, dia, 0, 0);        
        //convert o datetime para date
        Date dtVencimento = dt.toDate();    
        System.out.println(df.format(dtVencimento));
        //retorna a proxima data vencimento
       // return dtVencimento;
    }

/** exception */
Exception in thread "AWT-EventQueue-0" org.joda.time.IllegalFieldValueException: Value 14 for monthOfYear must be in the range [1,12]


/** I did this and now works */
/** cria data vencimento */    
    public Date getDataVencimento(Integer dia, Integer planoPagamento){
        //monta data para JodaTime
        DateTime data = DateTime.now();//pega data de hoje        
        DateTime d = data.plusMonths(planoPagamento);//adiciona plano de pagamento

        //cria data de vencimento
        DateTime vencimento = new DateTime(d.getYear(), d.getMonthOfYear(), dia, 0, 0);
        //convert o datetime para date
        Date dtVencimento = vencimento.toDate();            
        //retorna a proxima data vencimento
        return dtVencimento;
    }

我该如何解决这个问题?

【问题讨论】:

    标签: java date jodatime


    【解决方案1】:

    问题是,您将月份值传递给 DateTime 构造函数,该值不在范围内。如果溢出到下一个更高的字段,构造函数不会滚动值。如果任何字段超出范围,则会引发异常,这里的月份是14,肯定超出范围[1, 12]

    您可以简单地使用DateTime 类中的plusMonths() 方法来添加月份,而不是使用当前的方法:

    DateTime now = DateTime.now();
    DateTime sixMonthsLater = now.plusMonths(6);
    

    如果溢出,这将自动滚动月份值。

    【讨论】:

      【解决方案2】:

      data.getMonthOfYear() 将返回当前月份,August 表示 8 但您添加 6 表示 mes 变为 14 这不是一个有效的 MonthOfYear 因此导致 IllegalFieldValueException: Value 14

      你可以使用DateTime类的加号方法

      样本:

      DateTime dt = new DateTime(); //will initialized to the current time
      dt = dt.plusMonths(6); //add six month prior to the current date
      

      Documentation:

      plusMonths(int months) 
            Returns a copy of this datetime plus the specified number of months.
      

      【讨论】:

      • 请注意,DateTime 对象是不可变的。您对dt.plusMonths(6); 的调用不会影响dt 对象。您需要使用例如捕获结果dt = dt.plusMonths(6);
      【解决方案3】:

      最简单的方法是这样,通过使用plus 方法向DateTime 对象添加6 个月的周期:

      import org.joda.time.DateTime;
      import org.joda.time.Months;
      
      // ...
      
      DateTime now = new DateTime();
      DateTime sixMonthsLater = now.plus(Months.SIX);
      

      【讨论】:

      • @JeffMercado 原因是什么?
      • 啊,对不起,我误解了 Months 类中的常量代表什么。恰好定义了 12 个常量,我认为它们每个都代表月份数。 (与您可能认为的 Months.JANUARY 可能相反)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-21
      • 1970-01-01
      • 2012-03-31
      • 1970-01-01
      相关资源
      最近更新 更多