Java 如何使用JodaTime获取特定月份的最后日期?

Java 如何使用JodaTime获取特定月份的最后日期?,java,scala,jodatime,Java,Scala,Jodatime,我需要得到一个月的第一个日期(如org.joda.time.LocalDate)和最后一个日期。得到第一个是微不足道的,但得到最后一个似乎需要一些逻辑,因为月份有不同的长度,二月的长度甚至随年份而变化。JodaTime中是否已经内置了此机制,或者我应该自己实现它?如何: LocalDate endOfMonth = date.dayOfMonth().withMaximumValue(); dayOfMonth()返回一个LocalDate.Property,该属性表示“月日”字段,它知道原始

我需要得到一个月的第一个日期(如
org.joda.time.LocalDate
)和最后一个日期。得到第一个是微不足道的,但得到最后一个似乎需要一些逻辑,因为月份有不同的长度,二月的长度甚至随年份而变化。JodaTime中是否已经内置了此机制,或者我应该自己实现它?

如何:

LocalDate endOfMonth = date.dayOfMonth().withMaximumValue();
dayOfMonth()
返回一个
LocalDate.Property
,该属性表示“月日”字段,它知道原始
LocalDate

碰巧,
withMaximumValue()
方法甚至建议将其用于此特定任务:

此操作对于在月份的最后一天获取LocalDate非常有用,因为月份长度不同

LocalDate lastDayOfMonth = dt.dayOfMonth().withMaximumValue();

这是一个老问题,但在我寻找这个问题的时候,谷歌的搜索结果是最好的

如果有人需要实际的最后一天作为
int
而不是使用JodaTime,您可以这样做:

public static final int JANUARY = 1;

public static final int DECEMBER = 12;

public static final int FIRST_OF_THE_MONTH = 1;

public final int getLastDayOfMonth(final int month, final int year) {
    int lastDay = 0;

    if ((month >= JANUARY) && (month <= DECEMBER)) {
        LocalDate aDate = new LocalDate(year, month, FIRST_OF_THE_MONTH);

        lastDay = aDate.dayOfMonth().getMaximumValue();
    }

    return lastDay;
}
公共静态最终int一月=1;
公共静态最终int 12月=12;
公共静态最终int第一个月=1;
公共最终整数getLastDayOfMonth(最终整数月,最终整数年){
int lastDay=0;

如果使用JodaTime((月>=一月)&(月),我们可以这样做:

public static final Integer CURRENT_YEAR = DateTime.now().getYear(); public static final Integer CURRENT_MONTH = DateTime.now().getMonthOfYear(); public static final Integer LAST_DAY_OF_CURRENT_MONTH = DateTime.now() .dayOfMonth().getMaximumValue(); public static final Integer LAST_HOUR_OF_CURRENT_DAY = DateTime.now() .hourOfDay().getMaximumValue(); public static final Integer LAST_MINUTE_OF_CURRENT_HOUR = DateTime.now().minuteOfHour().getMaximumValue(); public static final Integer LAST_SECOND_OF_CURRENT_MINUTE = DateTime.now().secondOfMinute().getMaximumValue(); public static DateTime getLastDateOfMonth() { return new DateTime(CURRENT_YEAR, CURRENT_MONTH, LAST_DAY_OF_CURRENT_MONTH, LAST_HOUR_OF_CURRENT_DAY, LAST_MINUTE_OF_CURRENT_HOUR, LAST_SECOND_OF_CURRENT_MINUTE); } public static final Integer CURRENT_YEAR=DateTime.now().getYear(); public static final Integer CURRENT_MONTH=DateTime.now().getMonthOfYear(); 公共静态最终整数当前月份的最后一天=DateTime.now() .dayOfMonth().getMaximumValue(); public static final Integer LAST\u HOUR\u OF\u CURRENT\u DAY=DateTime.now() .hourOfDay().getMaximumValue(); public static final Integer LAST_MINUTE_OF_CURRENT_HOUR=DateTime.now().minuteof HOUR().getMaximumValue(); 公共静态最终整数LAST_SECOND_OF_CURRENT_MINUTE=DateTime.now().secondOfMinute().getMaximumValue(); 公共静态日期时间getLastDateOfMonth(){ 返回新的日期时间(当前年、当前月、, 本月的最后一天,本日的最后一小时, 最后一分钟(当前时间的最后一分钟,当前时间的最后一秒); }
正如我在github的小要点中所描述的:

另一个简单的方法是:

//Set the Date in First of the next Month:
answer = new DateTime(year,month+1,1,0,0,0);
//Now take away one day and now you have the last day in the month correctly
answer = answer.minusDays(1);

请注意,这也适用于
DateTime
types:)@Jon Skeet如何使用Java 8新的日期和时间API来获得它?@WarrenM.Nocos:我会使用
dt.with(temporaladjusts.lastDayOfMonth())
如果您的月份是12,那么会发生什么?JodaTime API是一个复杂的、完全加载的、方便的工作。有许多其他更正确的方法来实现这一点。如果您的月份是12,您知道最后一天是31,对吗?只需这样写:if(monthI希望有一个稍微简洁的答案,比如:public static int getLastDayOfMonth(int year,int month){LocalDate date=new LocalDate(year,month,1 return date.dayOfMonth().getMaximumValue();}但是您的答案非常有用,即使有点混乱,所以+1;)