如何使用 JodaTime 获取一年中的天数?
我尝试了以下方法但没有成功:
new Period(Years.ONE).getDays();
new Period(1, 0, 0, 000).getDays();
我想要的答案显然是365。
I've tried the following to no avail:
new Period(Years.ONE).getDays();
new Period(1, 0, 0, 000).getDays();
The answer I want is obviously 365.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您想要的答案显然不是
365
。它是365
或366
,您在示例中没有考虑闰年。由于某种原因,检测闰年并仅使用三元语句对其进行硬编码是不可接受的?
当然,这会给你当前年份的天数,如何获取不同年份的天数很简单,对读者来说是一个练习。
The answer you want isn't obviously
365
. It is either365
or366
, you don't take into account leap years in your example.Detecting a leap year and just hard coding it with a ternary statement would be unacceptable for some reason?
Of course this would give you the number of days in the current year, how to get number of days in a different year is trivial and a exercise for the reader.
如果您想要给定年份的实际天数:
当然,这会返回 365 或 366...通常:
If you want the real number of days for a given year:
Of course, this returns 365 or 366... normally:
tl;dr
使用 java.time
Joda-Time 项目,现在位于 维护模式,建议迁移到java.time 类。
java.time.Year
类可以代表任何特定年份。您可以查询有关该年的信息,例如天数长度或是否闰年 或不。
关于 java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
, & ;SimpleDateFormat
。Joda-Time 项目,现已在 维护模式,建议迁移到java.time 类。
要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310。
从哪里获取 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
间隔
,YearWeek
,YearQuarter
和 更多。tl;dr
Using java.time
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
The
java.time.Year
class can represent any particular year.You may interrogate for information about that year such as its length in number of days or whether it a Leap Year or not.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.new DateTime().year().toInterval().toDuration().getStandardDays();
new DateTime().year().toInterval().toDuration().getStandardDays();