爪哇岛一年中的儒略日
我在 http://www.rgagnon.com/javadetails/java 看到了“解决方案” -0506.html,但无法正常工作。例如,昨天(6 月 8 日)应该是 159,但它说是 245。
那么,有人在 Java 中有一个解决方案来获取当前日期的三位数儒略日(不是儒略日期 - 我需要今年的这一天)吗?
谢谢!标记
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
tl;dr
…或…
“儒略日”术语
术语“儒略日”有时被宽松地用来表示序号 一年中的某一天,或序数日期,含义1 到 365 或 366 之间的数字(闰年)。 1 月 1 日为
1
,1 月 2 日为2
,12 月 31 日为365
(闰年为366
)。这种对
Julian
的松散(不正确)使用可能来自 在天文学和其他跟踪日期领域中使用,作为自公元前 4713 年 1 月 1 日世界时中午以来的连续天数计数(在 儒略历)。如今,儒略日期已接近二百五十万,即今天的2,457,576
。java.time
Java 8 及更高版本中内置的 java.time 框架提供了一种简单的工具来获取一年中的某一天。
LocalDate
类表示仅日期值,没有时间和时区。您可以询问一年中的哪一天。转储到控制台。结果显示 2010 年 6 月 8 日确实是第 159 天。
时区对于确定日期至关重要。对于任何特定时刻,全球各地的日期都会因地区而异。例如,法国巴黎午夜过后几分钟就是新的一天,而魁北克蒙特利尔仍然是“昨天”。
走向另一个方向,从数字到日期。
org.thirden.extra.DayOfYear
ThreeTen-Extra 库向 Java 内置的 java.time 类添加了功能。
该库提供了一个类来明确表示任何一年的序数日:
DayOfYear
.使用此类而不是单纯的整数可以使您的代码更加自文档化,提供类型安全并确保有效值。获取特定年份的
DayOfYear
日期。关于 java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
< /a>, <代码>日历,&SimpleDateFormat< /代码>
。
要了解更多信息,请参阅 Oracle 教程 。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310。
Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。
您可以直接与数据库交换java.time对象。使用符合 JDBC 驱动程序 jeps/170" rel="noreferrer">JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* 类。 Hibernate 5 和 Hibernate 5 JPA 2.2 支持 java.time。
从哪里获取 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
Interval
,YearWeek
,YearQuarter
,以及更多。tl;dr
…or…
“Julian day” terminology
The term “Julian day” is sometimes used loosely to mean the ordinal day of the year, or Ordinal date, meaning a number from 1 to 365 or 366 (leap years). January 1 is
1
, January 2 is2
, December 31 is365
(or366
in leap years).This loose (incorrect) use of
Julian
probably comes from the use in astronomy and other fields of tracking dates as a continuous count of days since noon Universal Time on January 1, 4713 BCE (on the Julian calendar). Nowadays the Julian date is approaching two and half million,2,457,576
today.java.time
The java.time framework built into Java 8 and later provides an easy facility to get the day-of-year.
The
LocalDate
class represents a date-only value without time-of-day and without time zone. You can interrogate for the day-of-year.Dump to console. Results show that June 8, 2010 is indeed day # 159.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
Going the other direction, from a number to a date.
org.threeten.extra.DayOfYear
The ThreeTen-Extra library adds functionality to the java.time classes built into Java.
This library offers a class to represent explicitly the ordinal day of any year:
DayOfYear
. Using this class rather than a mere integer number makes your code more self-documenting, provides type-safety, and ensures valid values.Get a date for a
DayOfYear
with specific year.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
.To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for
java.sql.*
classes. Hibernate 5 & JPA 2.2 support java.time.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.如果您想要的只是一年中的某一天,为什么不使用 GregorianCalendars
DAY_OF_YEAR
字段呢?或者
,您可以计算今天的儒略日期和今年 1 月 1 日之间的差异。但请务必将结果加 1,因为 1 月 1 日不是一年中的第 0 天:
If all you want is the day-of-year, why don'you just use GregorianCalendars
DAY_OF_YEAR
field?}
Alternatively, you could calculate the difference between today's Julian date and that of Jan 1st of this year. But be sure to add 1 to the result, since Jan 1st is not the zeroth day of the year:
请注意,这没有考虑您引用的那个奇怪网站声称的“中午开始”交易。当然,只需检查时间就可以解决这个问题。
Note that this doesn't take into account the "starts at noon" deal claimed by that weird site you referenced. That could be fixed by just checking the time of course.
我已阅读所有帖子,但我认为有些内容不太清楚。
user912567提到了Jean Meeus,他是绝对正确的。
我发现的最准确的定义是Jean Meeus在其“天文算法”书中给出的(必须有,真的......)。
Julian Date 是一个日期,按照通常的方式表示,包含年、月和日。
儒略日是一个数字(实数),从 -4712 年开始计算,是“...连续的天数...”(以及天的分数)。
用于精确天文计算的有用时间尺度。
Jean Meeus:“儒略日与儒略历无关”(《天文算法》,第 2 版,第 59 页)
I've read all the posts and something's not very clear I think.
user912567 mentionned Jean Meeus, and he's absolutely right
The most accurate definition I've found is given by Jean Meeus in its "Astronomical Algorithms" book (a must have, really...).
Julian Date is a date, expressed as usual, with a year, a month and a day.
Julian Day is a number (a real number), counted from year -4712 and is "...a continuous count of days..." (and fraction of day).
A usefull time scale used for accurate astronomical calculations.
Jean Meeus : "The Julian Day has nothing to do with the Julian calendar" ("Astronomical Algorithms", 2nd Edition, p.59)
如果我们得到双朱利安日期,例如弦决策管理器
http://java.ittoolbox.com/groups/technical-function/java-l/java-function-to-convert-julian-date-to-calendar- date-1947446
以下内容正常工作,但第二个内容没有得到处理
如何在 Java 日期和 Julian 之间进行转换天数?
public static String julianDate(String julianDateStr) {
if we get a double julian date such as chordiant decision manager
http://java.ittoolbox.com/groups/technical-functional/java-l/java-function-to-convert-julian-date-to-calendar-date-1947446
The following is working but second is not taken care of
How can I convert between a Java Date and Julian day number?
public static String julianDate(String julianDateStr) {
如果您要查找自公元前 4713 年以来的天数中的儒略日,那么您可以请使用以下代码:
上面的代码基于 https://stackoverflow.com/a/9593736,因此受到限制迄今为止 1900 年至 2099 年。
If you're looking for Julian Day as in the day count since 4713 BC, then you can use the following code instead:
The above code is based on https://stackoverflow.com/a/9593736, and so is limited to dates between 1900 and 2099.
根据 @Basil Bourque 的回答,下面是我使用系统默认区域 ID 获取一年中的儒略日的实现。
Following @Basil Bourque answer, below is my implementation to get the Julian day of the year using system default Zone ID.
如果您真的只需要“一年中的某一天”,实际上,这只是公历/标准日历和 wallenborn 的答案是正确的。
但是,如果您关心实际的 Jualian-Day 或任何进一步的天文相关信息,我为此编写了一个小实用程序
https://github.com/wirklich-xyz/wirklich.xyz.astro/ blob/main/astro-time/src/main/java/xyz/wirklich/astro/time/JulianDay.java
也发布在 Maven 中心 xyz.wirklich.astro
随意使用它。这对于所有合理的用例来说都是非常精确的。
If you really just need "day of the year", indeed, this is just Gregorian/standard calendar and the answer of wallenborn is correct.
However, if you care for the actual Jualian-Day or any further astronomically-relevant information, I wrote a small utility for this
https://github.com/wirklich-xyz/wirklich.xyz.astro/blob/main/astro-time/src/main/java/xyz/wirklich/astro/time/JulianDay.java
which is also published on maven central at xyz.wirklich.astro
Feel free to use it. This is very precise for all reasonable use cases.
您还可以通过以下方式获取“儒略日期”或“序数日期”:
You can also get the "Julian Date" or "Ordinal Date" this way: