1

Requirement : get the date in Julian format (ordinal date), compare it with current date, get the month and year out of it.

Convert it with output format.

Input format: yydddd
Output format: yymm

JDK 8

One way I can do is using date :

Date myDateWrong = new SimpleDateFormat("yyyyddd").parse("2020366");

Any cleaner way ?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Jatin Bodarya
  • 1,425
  • 2
  • 20
  • 32
  • 2
    Do not lnger use the old `java.util.Date` and `SimpleDateFormat`. Use the newer `java.time.*`API – Jens Apr 29 '20 at 05:32
  • You have to format the date to your output format. At the moment you only parse it to a date object – Jens Apr 29 '20 at 05:34
  • Does this help? [How to convert Julian to Date in Java for format of “yyDHH”](https://stackoverflow.com/questions/30439571/how-to-convert-julian-to-date-in-java-for-format-of-yydhh) – Abra Apr 29 '20 at 05:41
  • 1
    looking for `java.time.*` solution which can parse yyyyddd format – Jatin Bodarya Apr 29 '20 at 06:00
  • 2
    FYI, the terribly flawed date-time classes such as [`java.util.Date`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/tutorial/datetime/TOC.html) classes built into Java 8 and later. – Basil Bourque Apr 29 '20 at 19:27
  • When you mean a day-of-year, I suggest you use the term [*ordinal date*](https://en.wikipedia.org/wiki/Ordinal_date). The term [*Julian date*](https://en.wikipedia.org/wiki/Julian_day) is ambiguous, as it can refer to pre-Gregorian calendar. – Basil Bourque Apr 29 '20 at 19:29

1 Answers1

3

java.time

looking for java.time.* solution which can parse yyyyddd format

That’s what I recommend too.

    DateTimeFormatter dayOfYearFormatter
            = DateTimeFormatter.ofPattern("uuuuDDD");
    DateTimeFormatter yearMonthFormatter
            = DateTimeFormatter.ofPattern("uuMM");

    String yyyydddString = "2020366";
    LocalDate date = LocalDate.parse(yyyydddString, dayOfYearFormatter);
    String output = date.format(yearMonthFormatter);

    System.out.println(output);

Output is:

2012

So year 2020 month 12.

What went wrong in your code?

Whether you use the modern DateTimeFormatter or the old and troublesome SimpleDateFormat, lowercase d is for day of month and uppercase D is for day of year. Why it worked with SimpleDateFormat anyway was because that class confusingly defaults month to January if no month is given. So your date was parsed into the 366th day of January. What?! That’s right, one more confusing trait of SimpleDateFormat, with default settings it happily parses non-existent dates. When there are only 31 days in January, it just extrapolates into the following months and ends up at December 31, the day you had intended. SimpleDateFormat is so full of nasty surprises like these. I recommend you never ever use that class again.

Link

Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161