如何在YearMonth对象中获取一个月的最后一天?

mem*_*und 2 java jodatime

我正在使用YearMonth对象joda.time,并希望在此对象中获取月份日期的最后一天.

yearMonth.monthOfYear().getMaximumValue(); //return 12 as the maximum month value is =12
Run Code Online (Sandbox Code Playgroud)

但我想要的是特定月份的最大日期?例如30或31.我怎么才能得到它?

fah*_*han 9

DateTime具有此现成功能.

DateTime dt = new DateTime();
System.out.println(dt.dayOfMonth().getMaximumValue());
Run Code Online (Sandbox Code Playgroud)

但是如果你想要它用于YearMonth,那么将YearMonth转换为DateTime并按上述方式执行:

YearMonth ym = new YearMonth();
dt = ym.toDateTime(null);
System.out.println(dt.dayOfMonth().getMaximumValue());
Run Code Online (Sandbox Code Playgroud)