Java 儒略日期到常规日期的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20035403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Julian date to regular date conversion
提问by Aarish Ramesh
How do i convert a julian date 2456606 which stands for Nov 18 2013 to the string format 18/11/2013 using java APIs? I tried executing the below code but it is not giving me the right answer. Any corrections to the below code are welcome
我如何使用 Java API 将代表 2013 年 11 月 18 日的儒略日期 2456606 转换为字符串格式 18/11/2013?我尝试执行下面的代码,但它没有给我正确的答案。欢迎对以下代码进行任何更正
String j = "2456606";
Date date = new SimpleDateFormat("yyyyD").parse(j);
String g = new SimpleDateFormat("dd.MM.yyyy").format(date);
System.out.println(g);
采纳答案by Nathaniel Jones
The Julian date for Nov 18 2013 is "2013322"
. The number you used, "2456606"
, would be the 606th day of 2456, which is Aug 28, 2457.
2013 年 11 月 18 日的儒略日期是"2013322"
。您使用的数字"2456606"
是 2456 的第 606 天,即 2457 年 8 月 28 日。
You might also have intended to use a different date format than "yyyyD"
for your input. See http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.htmlfor information on possible codes.
您可能还打算使用与"yyyyD"
输入不同的日期格式。有关可能的代码的信息,请参阅http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html。
Edit
编辑
The value that you used for the Julian date is the number of days since January 1, 4713 BCE. To get the Julian date using that system, you'll need to do something like the following:
您用于儒略日期的值是自公元前 4713 年 1 月 1 日以来的天数。要使用该系统获取 Julian 日期,您需要执行以下操作:
String j = "2456606";
int day = Integer.parseInt(j) - x; // x == Jan 1, 1970 on the Gregorian
j = Integer.toString(day);
Date date = new SimpleDateFormat("D").parse(j);
String g = new SimpleDateFormat("dd.MM.yyyy").format(date);
System.out.println(g);
Where x
is the Julian day corresponding to Jan 1, 1970 on the Gregorian calendar, i.e., the number of days elapsed between January 1, 4713 BCE and Jan 1, 1970.
x
对应于公历 1970 年 1 月 1 日的儒略日在哪里,即公元前 4713 年 1 月 1 日到 1970 年 1 月 1 日之间经过的天数。
回答by Basil Bourque
tl;dr
tl;博士
LocalDate.MIN.with (
java.time.temporal.JulianFields.MODIFIED_JULIAN_DAY ,
2_456_606L
)
2013-11-09
2013-11-09
Other direction, from modern date to Julian Day.
其他方向,从现代日期到儒略日。
LocalDate.of( 2013 , 11 , 9 )
.getLong ( java.time.temporal.JulianFields.JULIAN_DAY )
2456606
2456606
Details
细节
Firstly, your comment:
首先,您的评论:
the julian date to be 2456606 for nov 18 in the converter link mentioned below aa.usno.navy.mil/data/docs/JulianDate.php
在下面提到的转换器链接中,11 月 18 日的儒略日期为 2456606 aa.usno.navy.mil/data/docs/JulianDate.php
…is incorrect. That web site returns November 9, 2013 for 2456606.
…是不正确的。该网站在 2013 年 11 月 9 日返回 2456606。
Your Navy web sitedefines Julian Dateas a count of days since January 1, 4713 BC mapped to “Universal Time”. I assume they mean UTCand the modern ISO 8601 calendar system. See Wikipedia.
您的海军网站将Julian Date定义为自公元前 4713 年 1 月 1 日以来映射到“世界时”的天数。我认为它们是指UTC和现代 ISO 8601 日历系统。参见维基百科。
The java.time classes built into Java make this easy.
Java 内置的 java.time 类使这变得容易。
long input = 2_456_606L;
LocalDate ld = LocalDate.MIN.with ( java.time.temporal.JulianFields.JULIAN_DAY , input );
Dump to console.
转储到控制台。
System.out.println ( "input: " + input + " is: " + ld );
input: 2456606 is: 2013-11-09
输入:2456606 是:2013-11-09
For more discussion, see my Answeron a duplicate Question.
Going the other direction, converting a modern date to a Julian Day.
走向另一个方向,将现代日期转换为儒略日。
long output = ld.getLong ( java.time.temporal.JulianFields.JULIAN_DAY );
About java.time
关于 java.time
The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date
, Calendar
, & SimpleDateFormat
。
The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
Where to obtain the java.time classes?
从哪里获得 java.time 类?
- Java SE 8and SE 9and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABPproject adapts ThreeTen-Backport(mentioned above) for Android specifically.
- See How to use ThreeTenABP….
- Java SE 8和SE 9及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- 所述ThreeTenABP项目适应ThreeTen-反向移植(上述)为Android特异性。
- 请参阅如何使用ThreeTenABP ...。
The ThreeTen-Extraproject 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.
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。
回答by Smart Coder
If you have 7 digit julian date then you can use the following to convert it to Gergorian date.
The longJuliandate is your input and it'd return a String, which you can format for Date. You'd need to use "yyDDD" if you've 5 digit Julian date.
longJuliandate 是您的输入,它会返回一个字符串,您可以将其格式化为日期。如果您有 5 位儒略日期,则需要使用“yyDDD”。
DateFormat dt = new SimpleDateFormat("yyyyDDD");
try
{
Date date = dt.parse(longJulianDate); //2018038
String str = new SimpleDateFormat("yyyyMMdd").format(date);
} catch (ParseException e) {}