首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用JodaTime获取特定月份的最后日期?

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

Stack Overflow用户
提问于 2012-03-15 06:45:09
回答 2查看 60K关注 0票数 113

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

EN

回答 2

Stack Overflow用户

发布于 2016-07-01 07:51:38

另一个简单的方法是:

//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);
票数 3
EN

Stack Overflow用户

发布于 2016-01-15 01:10:34

这是一个古老的问题,但在我寻找这篇文章时,谷歌的搜索结果却是排名靠前的。

如果有人需要实际的最后一天作为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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9711454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档