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