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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9711454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:32:09  来源:igfitidea点击:

How to get the last date of a particular month with JodaTime?

javascalajodatime

提问by Ivan

I need to get the first date (as org.joda.time.LocalDate) of a month and the last one. Getting the first is trivial, but getting the last seems to need some logic as months have different length and February length even varies over years. Is there a mechanism for this already built in to JodaTime or should I implement it myself?

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

采纳答案by Jon Skeet

How about:

怎么样:

LocalDate endOfMonth = date.dayOfMonth().withMaximumValue();

dayOfMonth()returns a LocalDate.Propertywhich represents the "day of month" field in a way which knows the originating LocalDate.

dayOfMonth()返回一个LocalDate.Property代表在哪晓得原始的方式“月日”领域LocalDate

As it happens, the withMaximumValue()method is even documentedto recommend it for this particular task:

碰巧的是,该withMaximumValue()方法甚至被记录在案,以推荐它用于此特定任务:

This operation is useful for obtaining a LocalDate on the last day of the month, as month lengths vary.

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

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

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

回答by wiredniko

An old question, but a top google result when I was looking for this.

一个老问题,但是当我在寻找这个时,这是一个顶级的谷歌结果。

If someone needs the actual last day as an intinstead using JodaTime you can do this:

如果有人需要实际的最后一天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;
}

回答by Dassi Orleando

Using JodaTime,we can do this :

使用 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);
    }

As describe here in my small gist on github : A JodaTime and java.util.Date Util Class with a lot of usefull functions.

正如我在 github 上的小要点中所描述的: 具有许多有用功能的 JodaTime 和 java.util.Date Util 类。

回答by Abraham Maldonado Barrios

Another simple method is this:

另一种简单的方法是这样的:

//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);