2

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?

Ozturk
  • 569
  • 8
  • 20
  • 1
    I haven't worked with the regular Date classes in Java, so I can't help you with that. But I've always used the [Joda-Time library](http://www.joda.org/joda-time/), which does support the [Julian calendar](http://www.joda.org/joda-time/cal_julian.html). So you could consider using that if the native Java library doesn't work out for you. – D. Visser May 25 '15 at 13:39
  • 1
    First you should avoid to use java Date for Julian calendar. The Java Date API is broken, especially when it comes to complex time handling and computation such as with different calendars. Java 8 has a new time API that fixes those issue, where Date has been replaced by LocalDateTime. The Julian calendar is implemented in Java 8 by the Gregorian Calendar. https://docs.oracle.com/javase/8/docs/api/java/util/GregorianCalendar.html – Pierre-Antoine May 25 '15 at 14:10
  • Can you please clarify what you understand by the term "Julian"? There are several totally different meanings associated with (Julian calendar or Julian Day Number ). – Meno Hochschild May 27 '15 at 10:36
  • @Pierre I find your comment strange. First you complain about the old Java support for date and time. Then you recommend using Java-8 and the new `java.time`-package there. Finally you go back to the old Java-stuff recommending `java.util.GregorianCalendar` which has nothing to do with new time library introduced in Java-8. I find it confusing. And by the way: The new replacement for `java.util.Date` is `java.time.Instant`. – Meno Hochschild May 27 '15 at 10:40

4 Answers4

2

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.

arcy
  • 12,845
  • 12
  • 58
  • 103
1

your format is incorrect. You should use:

Date myDateWrong = new SimpleDateFormat("yydddhh").parse("1515010");
avk
  • 871
  • 1
  • 9
  • 22
0

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);
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
0

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154