Java 乔达时间:每月15日和最后一天

Java 乔达时间:每月15日和最后一天,java,datetime,jodatime,Java,Datetime,Jodatime,我怎样才能在一个循环中得到Joda time中的每月15日和最后一天 这是代码,看起来像: DateTime initDate = new DateTime(2015,1,31,0,0); for(int i = 0; i > 4; i++){ initDate = initDate.plusDays(15); System.out.println(initDate.toString("yyyy-MM-dd"); } 我只希望结果是这样的: 2015-2-15 2015-2

我怎样才能在一个循环中得到Joda time中的每月15日和最后一天

这是代码,看起来像:

DateTime initDate = new DateTime(2015,1,31,0,0);

for(int i = 0; i > 4; i++){
  initDate = initDate.plusDays(15);

   System.out.println(initDate.toString("yyyy-MM-dd");
}
我只希望结果是这样的:

2015-2-15
2015-2-28
2015-3-15
2015-3-30

这个循环很好,但是您可以直接添加一个月,而不是添加天数(查看和 )而不是得到一个月的最后一天,你可以使用

yourcurrentDate.dayOfMonth().withMaximumValue()
for(inti=0;i<4;i++){
initDate=initDate.plusMonths(1);
initDate=initDate.withDayOfMonth(15);
System.out.println(initDate.toString(“yyyy-MM-dd”);
System.out.println(initDate.dayOfMonth().withMaximumValue().toString(“yyyy-MM-dd”);
}

类似的情况?

您不能通过添加固定的天数来获得一个月的最后一天,因为并非所有月份的天数都相同

相反,像这样使用循环

for (each month) {
   set the month appropriately (set not add)
   get the 15th day of this month (set not add)
   print the 15th day
   set the next month
   set the 1st day of the "next" month
   subtract a day
   print the "last" day
}

这肯定会有帮助:可能不会做你认为它会做的事情。毕竟,二月的最大值是29日,只有25%的时间是正确的。谢谢,先生。“这真的很有帮助。”埃德温巴克回答是正确的。使用MaximumValue()的(优雅的)方法
取决于上下文,因为您可以轻松地验证自己。
for (each month) {
   set the month appropriately (set not add)
   get the 15th day of this month (set not add)
   print the 15th day
   set the next month
   set the 1st day of the "next" month
   subtract a day
   print the "last" day
}