【发布时间】: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;
}
我该如何解决这个问题?
【问题讨论】: