Java 如何获取YearMonth对象中一个月的最后一天?
我正在使用Java 如何获取YearMonth对象中一个月的最后一天?,java,jodatime,Java,Jodatime,我正在使用joda.time的YearMonth对象,希望获取该对象中月份日期的最后一天 yearMonth.monthOfYear().getMaximumValue(); //return 12 as the maximum month value is =12 但我想要的是该月的最长天数?大多是30或31岁。如何获取它?如果您想在Joda中获取当月的最长日期,您应该使用LocalDate 例如: 输出: 2014-10-31 从 从yearMonth创建DateTime对象: Date
joda.time
的YearMonth
对象,希望获取该对象中月份日期的最后一天
yearMonth.monthOfYear().getMaximumValue(); //return 12 as the maximum month value is =12
但我想要的是该月的最长天数?大多是30或31岁。如何获取它?如果您想在
Joda
中获取当月的最长日期,您应该使用LocalDate
例如:
输出:
2014-10-31
从
从yearMonth创建DateTime对象:
DateTime datetime = new DateTime(yearMonth.get(DateTimeFieldType.year()), yearMonth.get(DateTimeFieldType.monthOfYear()),1,0,0);
System.out.println(datetime.dayOfMonth().getMaximumValue());
DateTime有这个现成的特性
DateTime dt = new DateTime();
System.out.println(dt.dayOfMonth().getMaximumValue());
但如果您想将其用于YearMonth,则将YearMonth转换为DateTime,并执行上述操作:
YearMonth ym = new YearMonth();
dt = ym.toDateTime(null);
System.out.println(dt.dayOfMonth().getMaximumValue());
重复他使用jodatime而不是Calendar类询问的@Jayram的可能重复项。所以它不是duplicate@Jayram这是一个关于乔达时间的问题。不要怀疑@Jens我有一个
YearMonth
对象,它没有.dayOfMonth()
方法。不要怀疑!如上所述,我只有YearMonth
对象。我没有LocalDate
。。。
DateTime dt = new DateTime();
System.out.println(dt.dayOfMonth().getMaximumValue());
YearMonth ym = new YearMonth();
dt = ym.toDateTime(null);
System.out.println(dt.dayOfMonth().getMaximumValue());