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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:31:47  来源:igfitidea点击:

Julian date to regular date conversion

javadatejulian-date

提问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 xis 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。

Screen shot of US Naval Observatory reporting 2013-11-09 as the modern date for a Julian Day of 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 类?

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 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多

回答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) {}