2

I need to convert new Date() to Julian date format.is there is any build in java function for this. my exact requirement is

Represents the creation date of the file in Julian date format (0YYDDD): 0 – numeric zero YY – last two digits of the year DDD – day number within the year Can be up to 7 calendar days before the date of transmission Example: 010163 = June 11, 2010

What is really looking is some thing like this

Date date=new Date();
String JulianDtae=date.someFunction()

Any help will be appreciated

abhi
  • 794
  • 5
  • 15
  • 28
  • 1
    Look into `SimpleDateFormat` and come back should you encounter any issue with that. – dotvav Aug 19 '15 at 09:09
  • What *exactly* are you expecting for "Julian date format"? (If your variable were a `double`, that would make more sense to me...) – Jon Skeet Aug 19 '15 at 09:09
  • @Jon Skeet June 11, 2010 =010163 = this type of format – abhi Aug 19 '15 at 09:11
  • 2
    @abhi: What's the correlation between June 11th 2010 and "010163"? Is that meant to just be a number, or a formatted string? Please give more information... (A reference to the rules you're trying to implement would be useful. Please bear in mind that as I mentioned, "Julian date" and "Julian date format" have multiple meanings.) – Jon Skeet Aug 19 '15 at 09:14
  • @Jon Skeet Represents the creation date of the file in Julian date format (0YYDDD): 0 – numeric zero YY – last two digits of the year DDD – day number within the year Can be up to 7 calendar days before the date of transmission Example: 010163 = June 11, 2010 – abhi Aug 19 '15 at 09:22
  • Related question: http://stackoverflow.com/questions/1171208/what-is-the-precise-definition-of-jdes-julian-date-format. Answers include conversion code in various languages that should be trivial tor translate into Java. – Stephen C Aug 19 '15 at 09:29
  • 2
    @abhi: Right. It would have been useful to include that in the question. – Jon Skeet Aug 19 '15 at 09:34
  • Actually, June 11 is the 192nd day of 2010, not the 163rd one. – clapsus Aug 19 '15 at 13:45
  • @clapsus A typo? It’s the 162nd day of that year (in a leap year June 11 is the 163rd day, but 2010 was no leap year). – Ole V.V. Jan 20 '21 at 17:51
  • 1
    Update: The `Date` class seen in this Question is now legacy, supplanted by the *java.time* classes defined in JSR 310. For a modern solution, see the [Answer by Ole V.V.](https://stackoverflow.com/a/65814593/642706). – Basil Bourque Jan 21 '21 at 07:59

3 Answers3

4

java.time

I recommend that you use java.time, the modern Java date and time API, for your date work. The format you need is built in.

    LocalDate today = LocalDate.now(ZoneId.systemDefault());
    String ordinalDateString = today.format(DateTimeFormatter.ISO_ORDINAL_DATE);
    System.out.println(ordinalDateString);

Output for today January 20, 2021 in standard ISO 8601 format:

2021-020

The format you mention, 0YYDDD, is peculiar. It’s nothing I have seen before. If you’re serious about it, define a formatter that gives it:

    DateTimeFormatter peculiarDateFormatter = DateTimeFormatter.ofPattern("0uuDDD");

021020

If the first three digits, 021, were supposed to be the last three digits of the year, java.time can do that too. It requires a few more words:

    DateTimeFormatter peculiarDateFormatter = new DateTimeFormatterBuilder()
            .appendValueReduced(ChronoField.YEAR, 3, 3, 1950)
            .appendValue(ChronoField.DAY_OF_YEAR, 3)
            .toFormatter();

For the date in 2021 output is the same as before. For dates in other centuries the first digit will no longer be 0.

Ordinal date, not Julian date

The day number of the year that you ask for is called the ordinal date, which is why the built-in formatter also has ordinal in its name. A Julian day is something else, the continuous count of days since January 1, 4713 BCE. The ordinal date is sometimes referred to as Julian, but there is nothing genuinely Julian about it, so to avoid confusion, prefer ordinal over Julian.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    I've come across this "peculiar" format parsing a date from a Canadian bank data file. Not sure why they insist on the '0' in the front. You're correct that it's an ordinal date, as noted with this converter: http://www.longpelaexpertise.com/toolsJulian.php _"A yyddd (or similar format such as yyyyddd, yy-ddd) date is more correctly called an ordinal date. However in the mainframe world, we call them 'Julian Dates', and so we've used this term on this page."_ – trilogy Mar 24 '23 at 12:59
2

Use SimpleDateFormat.

The following code returns the Julian date string for date according to the format you gave.

String julianDateString = new SimpleDateFormat("'0'yyD").format(date);
clapsus
  • 442
  • 6
  • 19
  • 2
    Update: These classes are now legacy, supplanted by the *java.time* classes defined in JSR 310. – Basil Bourque Jan 21 '21 at 07:53
  • Also wrong. For the date mentioned in the question, June 11, 2010, it gives the expected result. For today’s date I expect `023083`, but I got `02383`, 5 digits instead of 6. – Ole V.V. Mar 24 '23 at 15:45
0

Actually I think what you need is

String yearYy = new SimpleDateFormat("yy").format(today)
String dayD = new SimpleDateFormat("D").format(today)
String dayDDD = dayD.padLeft(3,'0')
String julianDateString = yearYy + dayDDD

This gives the proper Julian date format - there shouldn't be a leading '0', but you do need to pad the day number so that it's always 3 characters.

...I'm quite sure this could be simplified, but the important thing is that the day number should be padded.

So 20/01/21 gives 21020 (rather than 02120 when using the previous example)

ianf
  • 21
  • 3
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 20 '21 at 17:22
  • Also I cannot compile your code. After adding semicolons I get *Cannot resolve method 'padLeft' in 'String'*. I am using Java 18. – Ole V.V. Mar 24 '23 at 15:41