提问人:Saeed Noshadi 提问时间:1/18/2022 更新时间:1/18/2022 访问量:500
我怎样才能在“JODA”库中获取每个月的天数列表?
How can I get the list of days of each month in the "JODA" library?
答:
2赞
Basil Bourque
1/18/2022
#1
TL的;博士
yearMonth // An instance of `java.time.YearMonth`.
.atDay( 1 ) // Returns a `LocalDate` object for the first of the month.
.datesUntil( // Get a range of dates.
yearMonth
.plusMonths( 1 ) // Move to the following month.
.atDay( 1 ) // Get the first day of that following month, a `LocalDate` object.
) // Returns a stream of `LocalDate` objects.
.toList() // Collects the streamed objects into a list.
对于没有方法的旧版本的 Java,请使用 。Stream#toList
collect( Collectors.toList() )
java.time
Joda-Time项目现在处于维护模式。该项目建议迁移到其后继者,即 JSR 310 中定义并内置于 Java 8 及更高版本中的 java.time 类。Android 26+ 有一个实现。对于早期的 Android,最新的 Gradle 工具通过“API 脱糖”提供了大部分 java.time 功能。
YearMonth
指定月份。
YearMonth ym = YearMonth.now() ;
询问它的长度。
int lengthOfMonth = ym.lengthOfMonth() ;
LocalDate
若要获取日期列表,请获取该月的第一个日期。
LocalDate start = ym.atDay( 1 ) ;
下个月的第一天。
LocalDate end = ym.plusMonths( 1 ).atDay( 1 ) ;
获取中间的日期流。收集到列表中。
List< LocalDate > dates = start.datesUntil( end ).toList() ;
评论
0赞
Saeed Noshadi
1/18/2022
我的最低版本是 21 岁。java.time 有一个错误
0赞
Basil Bourque
1/18/2022
@saeednoshadi 正如我所说,您需要使用最新的 Gradle 工具。搜索“API 脱糖”以了解更多信息。如果这对您不起作用,请在 ThreeTenABP 库中添加 Android 适配的 back-port。
0赞
Saeed Noshadi
1/18/2022
我可以使用 DateTime 类获取月份名称列表吗?
0赞
Basil Bourque
1/18/2022
@saeednoshadi 月#getDisplayName
。我建议你花一些时间仔细阅读java.time类。并阅读 Oracle 的免费教程。
评论