How to convert Julian to Date in Java for format of “yyDHH”

ぃ、小莉子 提交于 2021-02-08 06:23:05

问题


yy = 15 (year), D = 150 (day of year), HH = 10 (hour)

 Date myDateWrong = new SimpleDateFormat("yyDHH").parse("1515010");
 Date myDateTrue = new SimpleDateFormat("yyD").parse("15150");

myDateTrue is right: 30/05/2015. myDateWrong must be 30/05/2015 10:00:00 but it seem that 28/07/2015 18:00:00. Whats the problem in here?


回答1:


I'm going to guess that the wrongDate took a single digit for the month (as you specified) and then took the rest of the digits for the hour (much as it took the rest of the digits for the julian date from the original format). So you got 2015, January, and 5010 hours. I haven't done the calculations in detail, but 5010 hours would give you roughly 7 months, which would explain ending up in July. 18:00 hours just reflects whatever hours were left over.

So you need to use DDD as the specifier for the Julian day (not ddd as suggested in another comment), and then it comes out as expected.




回答2:


your format is incorrect. You should use:

Date myDateWrong = new SimpleDateFormat("yydddhh").parse("1515010");



回答3:


You need 15150 to be 30/05/2015... but actually 12:00:00 Try this:

String j = "15150";
Date date = new SimpleDateFormat("yyD").parse(j);
String g = new SimpleDateFormat("dd.MM.yyyy hh:mm").format(date);
System.out.println(g);



回答4:


tl;dr

LocalDateTime ldt = LocalDateTime.parse ( "1515010" , DateTimeFormatter.ofPattern ( "yyDHH" ) );
LocalDate ld = LocalDate.parse ( "15150" , DateTimeFormatter.ofPattern ( "yyD" ) );

Not Julian… Ordinal Date

Using the term “Julian” for an ordinal day-of-year number is not technically correct but is commonly used nonetheless. I suggest you avoid the ambiguity and confusion with an actual Julian date and stick with the accurate term ordinal date or “day-of-year”.

java.time

The java.time framework built into Java 8 and later can help here. Example code below proves that both your scenarios (with and without hour-of-day) work correctly in java.time.

These java.time classes supplant the old troublesome date-time classes such as java.util.Date. See Oracle Tutorial. Much of the functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

Define a formatter pattern with the DateTimeFormatter class. The formatting codes are similar to those of SimpleDateFormat, but not exactly the same, so study the class documentation. With this class, if the century is omitted, the 21st century (20xx) is assumed.

String input = "1515010";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "yyDHH" );

LocalDateTime ldt = LocalDateTime.parse ( input , formatter );

A LocalDateTime lacks any time zone or offset-from-UTC information. So it does not represent a moment on the timeline. If we can assume this input was intended to be in the context of UTC time zone, then convert to a OffsetDateTime.

OffsetDateTime odt = ldt.atOffset ( ZoneOffset.UTC );

Dump to console.

System.out.println ( "input: " + input + " | ldt: " + ldt + " | odt: " + odt );

input: 1515010 | ldt: 2015-05-30T10:00 | odt: 2015-05-30T10:00Z

For a date-only value without time-of-day and without time zone, we instantiate a LocalDate.

String input = "15150";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "yyD" );
LocalDate ld = LocalDate.parse ( input , formatter );

Dump to console.

System.out.println ( "input: " + input + " | ld: " + ld );

input: 15150 | ld: 2015-05-30



来源:https://stackoverflow.com/questions/30439571/how-to-convert-julian-to-date-in-java-for-format-of-yydhh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!