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

后端 未结 4 1206
礼貌的吻别
礼貌的吻别 2020-12-02 11:36

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 log

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 12:19

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

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

    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;
    }
    

提交回复
热议问题