如何使用JodaTime获取特定月份的最后日期? [英] How to get the last date of a particular month with JodaTime?

查看:823
本文介绍了如何使用JodaTime获取特定月份的最后日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

如何:

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

dayOfMonth()返回 LocalDate.Property 代表day of month字段,知道原始 LocalDate

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

碰巧, withMaximumValue()方法甚至是记录以推荐此特定任务:

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


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

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

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


这篇关于如何使用JodaTime获取特定月份的最后日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆