2

Input - Julian Date - "7107" Output - Calendar Date I tried the following code

String julianDate = "7107";
SimpleDateFormat fmt1 = new SimpleDateFormat("yDDD");
Date date = fmt1.parse(julianDate);
SimpleDateFormat fmt2 = new SimpleDateFormat("MM/dd/yyyy");
String output = fmt2.format(date);
System.out.println("Calendar Date : " + output); 

But getting wrong output as "Calendar Date : 04/17/0007"

So since folks ask how that single "7" represent year. So even if I give "2017107" as input, representing "YYYYDDD" format . Even then I am getting wrong output.

My expected output for "2017107" julian date is "04/17/2017" .But output is Calendar Date : 11/01/0048

Used Java 7 version

Jaya Lakshmi
  • 57
  • 1
  • 3
  • 12
  • What date should 7107 be? `yDDD` would mean year 7 and day 107 in that year which is April 17th (day 107 in a non-leapyear) of the year 7. – Thomas Apr 19 '17 at 10:33
  • Possible duplicate of [Convert a Julian Date to Regular Calendar Date](http://stackoverflow.com/questions/3017954/convert-a-julian-date-to-regular-calendar-date) – LazerBanana Apr 19 '17 at 10:33
  • if java 8 you can use code described in this answer http://stackoverflow.com/a/36716991/4664558 – LazerBanana Apr 19 '17 at 10:35
  • 1
    what is the date you actually expected? – Kushan Apr 19 '17 at 10:43
  • @InjuredThePatient No, you linked to a Question dealing with "Julian" that means a count of days since -4713-11-24 Gregorian. I suspect this Question is about day-of-year, 1-366 (a common misuse of the term "Julian"). – Basil Bourque Apr 19 '17 at 10:48
  • @BasilBourque I see. Jala Lakshmi which Java version is used here ? – LazerBanana Apr 19 '17 at 10:53
  • By “yDDD", do you mean a single digit for year and three digits for day-of-year 1-366? If so, how do your translate that single digit to a year? – Basil Bourque Apr 19 '17 at 10:55

2 Answers2

1

Try this,

Date date = new SimpleDateFormat("yyyyD").parse("2017107");

Date getYear() is deprecate now.

Use this way to get year,

int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
Kushan
  • 10,657
  • 4
  • 37
  • 41
  • Thanks Kushan. The output is correct for this loc, but to get in "MM/dd/YYYY" format if I use date.getYear() then its giving wrong output . date.getYear() gives 117. – Jaya Lakshmi Apr 19 '17 at 11:18
0

Ordinal date

The technical term for describing a date as a year and a day-of-year (1-366) is Ordinal Date.

Define your formatting pattern using uppercase D for day-of-year 1-366.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuDDD" );
LocalDate ld = LocalDate.parse( "2017107" , f );

Generate a string in that format.

String output = ld.format( f );

ISO 8601

I suggest sticking to the “expanded” format standard ISO 8601 format if at all possible: YYYY-DDD such as 2017-107. You are using the “basic” version of the standard format which is less readable and less obvious than the expanded version.

Back-port

Much of the java.time functionality is back-ported to Java 6 & 7 in the ThreeTen-Backport project.

Extensions

Tip: You may find helpful the DayOfYear class in the ThreeTen-Extra project when you move to Java 8 or 9. That project extends the java.time classes to add functionality.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thanks Bourque. But I am using Java 7. DateTimeFormatter and LocalDate are not supported in it. – Jaya Lakshmi Apr 19 '17 at 11:01
  • Uh, questioner is using Java 7 -- wasn't DateTimeFormatter introduced in Java 8? – John Hascall Apr 19 '17 at 11:04
  • @JohnHascall Much of the java.time functionality is back-ported to Java 6 & 7 in the [*ThreeTen-Backport*](http://www.threeten.org/threetenbp/) project. Well worth the bother of adding a library to your project, as the legacy date-time classes are an awful mess. – Basil Bourque Apr 19 '17 at 11:06