问题
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
String j = "2456606";
Date date = new SimpleDateFormat("yyyyD").parse(j);
String g = new SimpleDateFormat("dd.MM.yyyy").format(date);
System.out.println(g);
回答1:
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.
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.html for information on possible codes.
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:
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.
回答2:
tl;dr
LocalDate.MIN.with (
java.time.temporal.JulianFields.MODIFIED_JULIAN_DAY ,
2_456_606L
)
2013-11-09
Other direction, from modern date to Julian Day.
LocalDate.of( 2013 , 11 , 9 )
.getLong ( java.time.temporal.JulianFields.JULIAN_DAY )
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
…is incorrect. That web site returns November 9, 2013 for 2456606.
Your Navy web site defines Julian Date as a count of days since January 1, 4713 BC mapped to “Universal Time”. I assume they mean UTC and the modern ISO 8601 calendar system. See Wikipedia.
The java.time classes built into Java make this easy.
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
For more discussion, see my Answer on 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
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?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
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.
回答3:
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.
DateFormat dt = new SimpleDateFormat("yyyyDDD");
try
{
Date date = dt.parse(longJulianDate); //2018038
String str = new SimpleDateFormat("yyyyMMdd").format(date);
} catch (ParseException e) {}
来源:https://stackoverflow.com/questions/20035403/julian-date-to-regular-date-conversion