如何使用 JodaTime 获取一年中的天数?

发布于 2025-01-05 11:08:34 字数 139 浏览 4 评论 0原文

我尝试了以下方法但没有成功:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月苍老的讽刺 2025-01-12 11:08:34

您想要的答案显然不是365。它是 365366,您在示例中没有考虑闰年。

由于某种原因,检测闰年并仅使用三元语句对其进行硬编码是不可接受的?

final DateTime dt = new DateTime();
final int daysInYear = dt.year().isLeap() ? 366 : 365;

当然,这会给你当前年份的天数,如何获取不同年份的天数很简单,对读者来说是一个练习。

The answer you want isn't obviously 365. It is either 365 or 366, 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?

final DateTime dt = new DateTime();
final int daysInYear = dt.year().isLeap() ? 366 : 365;

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.

帅哥哥的热头脑 2025-01-12 11:08:34

如果您想要给定年份的实际天数:

int year = 2012;
LocalDate ld = new LocalDate(year,1,1);
System.out.println(Days.daysBetween(ld,ld.plusYears(1)).getDays());

当然,这会返回 365 或 366...通常:

int year = 1582;
LocalDate ld = new LocalDate(year,1,1,GJChronology.getInstance());
System.out.println(Days.daysBetween(ld,ld.plusYears(1)).getDays());
// year 1582 had 355 days

If you want the real number of days for a given year:

int year = 2012;
LocalDate ld = new LocalDate(year,1,1);
System.out.println(Days.daysBetween(ld,ld.plusYears(1)).getDays());

Of course, this returns 365 or 366... normally:

int year = 1582;
LocalDate ld = new LocalDate(year,1,1,GJChronology.getInstance());
System.out.println(Days.daysBetween(ld,ld.plusYears(1)).getDays());
// year 1582 had 355 days
千紇 2025-01-12 11:08:34

tl;dr

java.time.Year.of( 2017 ).length()

使用 java.time

Joda-Time 项目,现在位于 维护模式,建议迁移到java.time 类。

java.time.Year 类可以代表任何特定年份。

Year year = Year.of( 2017 );

您可以查询有关该年的信息,例如天数长度或是否闰年 或不。

int countDaysInYear = year.length() ;
boolean isLeapYear = year.isLeap() ; // ISO proleptic calendar system rules.

关于 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 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如 间隔YearWeekYearQuarter更多

tl;dr

java.time.Year.of( 2017 ).length()

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.

Year year = Year.of( 2017 );

You may interrogate for information about that year such as its length in number of days or whether it a Leap Year or not.

int countDaysInYear = year.length() ;
boolean isLeapYear = year.isLeap() ; // ISO proleptic calendar system rules.

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.

离不开的别离 2025-01-12 11:08:34

new DateTime().year().toInterval().toDuration().getStandardDays();

new DateTime().year().toInterval().toDuration().getStandardDays();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文