Code · Java & C#

Julian dates in Java and C#

Both kinds of “Julian date” in Java (java.time) and C#/.NET: the day-of-year code (YYYYDDD, YYDDD and JD Edwards CYYDDD) in both directions, and the astronomical Julian Day Number, Julian Date and MJD.

Two meanings of “Julian date”

The day-of-year Julian date is a year plus the day number: 2026202 (YYYYDDD) or 26202 (YYDDD). The astronomical Julian Date counts days since noon UTC on January 1, 4713 BC, for example 2461242.5. They share a name but nothing else, so this page keeps them apart.

Java: day of year and YYYYDDD

LocalDate.getDayOfYear() returns the day number. In DateTimeFormatter patterns, D is the day of year and DDD pads it to three digits:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

LocalDate d = LocalDate.of(2026, 7, 21);

int doy = d.getDayOfYear();                                    // 202
int yyyyddd = d.getYear() * 1000 + d.getDayOfYear();           // 2026202

// As text: 'D' is day of year, "DDD" pads it to three digits
String s7 = d.format(DateTimeFormatter.ofPattern("uuuuDDD"));  // "2026202"
String s5 = d.format(DateTimeFormatter.ofPattern("uuDDD"));    // "26202"

// Today in a given time zone
int today = LocalDate.now(java.time.ZoneId.of("America/Chicago")).getDayOfYear();

YYYY is not the year

Capital YYYY is the week-based year. It prints the wrong year for a few days around New Year, which is exactly when Julian codes roll over from 365 to 001. Use uuuu (or yyyy) in java.time and yyyy in SimpleDateFormat.

Parse a code back to a date

LocalDate.ofYearDay(year, dayOfYear) is the direct route:

// From a number: no formatter needed
LocalDate a = LocalDate.ofYearDay(2026202 / 1000, 2026202 % 1000);   // 2026-07-21

// From text
LocalDate b = LocalDate.parse("2026202", DateTimeFormatter.ofPattern("uuuuDDD"));  // 2026-07-21

// YYDDD with your own century rule (here 00-68 = 2000s)
int c = 26202, yy = c / 1000;
LocalDate e = LocalDate.ofYearDay((yy <= 68 ? 2000 : 1900) + yy, c % 1000);        // 2026-07-21

JD Edwards (CYYDDD)

// JD Edwards CYYDDD (C = centuries since 1900); 0 means "no date"
static LocalDate fromJde(int jde) {
    return jde == 0 ? null : LocalDate.ofYearDay(1900 + jde / 1000, jde % 1000);
}
static int toJde(LocalDate d) {
    return (d.getYear() - 1900) * 1000 + d.getDayOfYear();
}
// fromJde(126202) -> 2026-07-21, fromJde(99365) -> 1999-12-31

Java: Julian Day Number, JD and MJD

java.time.temporal.JulianFields has the Julian Day Number and the MJD built in, in both directions. For a Julian Date with a time of day, convert from an Instant:

import java.time.Instant;
import java.time.LocalDate;
import java.time.temporal.JulianFields;

LocalDate d = LocalDate.of(2026, 7, 21);
long jdn = d.getLong(JulianFields.JULIAN_DAY);            // 2461243 (Julian Day Number)
long mjd = d.getLong(JulianFields.MODIFIED_JULIAN_DAY);   // 61242

LocalDate back = LocalDate.MIN.with(JulianFields.JULIAN_DAY, 2461243);   // 2026-07-21

// Julian Date with the time of day, from an Instant (always UTC)
double jd = Instant.parse("2026-07-21T18:00:00Z").toEpochMilli() / 86_400_000.0 + 2440587.5;  // 2461243.25
double jdNow = Instant.now().toEpochMilli() / 86_400_000.0 + 2440587.5;

JULIAN_DAY is the number for the whole calendar date; the Julian Date at midnight UTC is half a day less (2461242.5). Java applies the Gregorian calendar to every date, so before 1582 its values differ from astronomical tables.

C# (.NET): day of year and YYYYDDD

DateTime.DayOfYear and DateOnly.DayOfYear give the day number. .NET format strings have no day-of-year specifier, so build the text from the year and a "D3"-padded day:

DateTime d = new DateTime(2026, 7, 21);

int doy = d.DayOfYear;                               // 202
int yyyyddd = d.Year * 1000 + d.DayOfYear;           // 2026202

// .NET format strings have no day-of-year specifier, so build the text
string s7 = d.ToString("yyyy") + d.DayOfYear.ToString("D3");   // "2026202"
string s5 = d.ToString("yy") + d.DayOfYear.ToString("D3");     // "26202"

// DateOnly (.NET 6+) has the same property
int today = DateOnly.FromDateTime(DateTime.Now).DayOfYear;

Parse a code back to a date, and JD Edwards

DateTime.ParseExact cannot read a day of year, so split the number and add days to January 1:

// From a number
int c = 2026202;
DateTime a = new DateTime(c / 1000, 1, 1).AddDays(c % 1000 - 1);     // 2026-07-21

// From text
string s = "2026202";
DateTime b = new DateTime(int.Parse(s[..4]), 1, 1).AddDays(int.Parse(s[4..]) - 1);

// JD Edwards CYYDDD; 0 means "no date"
static DateOnly? FromJde(int jde) =>
    jde == 0 ? null : new DateOnly(1900 + jde / 1000, 1, 1).AddDays(jde % 1000 - 1);
static int ToJde(DateOnly d) => (d.Year - 1900) * 1000 + d.DayOfYear;

C# (.NET): Julian Day Number, JD and MJD

DateOnly.DayNumber counts days from January 1 of year 1, so a constant turns it into a Julian Day Number. For a Julian Date with the time, start from a DateTimeOffset so the time zone is never in doubt:

// Julian Day Number from DateOnly (.NET 6+): DayNumber counts from 0001-01-01
DateOnly d = new DateOnly(2026, 7, 21);
int jdn = d.DayNumber + 1721426;                       // 2461243
DateOnly back = DateOnly.FromDayNumber(2461243 - 1721426);   // 2026-07-21

// Julian Date with the time of day, from a UTC instant
static double JulianDate(DateTimeOffset t) =>
    t.ToUnixTimeMilliseconds() / 86_400_000.0 + 2440587.5;

double jd  = JulianDate(new DateTimeOffset(2026, 7, 21, 18, 0, 0, TimeSpan.Zero));  // 2461243.25
double mjd = jd - 2400000.5;                                                         // 61242.75
double now = JulianDate(DateTimeOffset.UtcNow);

// JD back to a UTC DateTimeOffset
static DateTimeOffset FromJulianDate(double jd) =>
    DateTimeOffset.FromUnixTimeMilliseconds((long)Math.Round((jd - 2440587.5) * 86_400_000));

Traps in .NET

  • DateTime.ToOADate() counts days from December 30, 1899. It is not a Julian Date, though for UTC values after 1900 adding 2415018.5 turns it into one.
  • A DateTime with Kind = Unspecified is treated as local time by ToUniversalTime(). Prefer DateTimeOffset for instants.
  • System.Globalization.JulianCalendar is the old Julian calendar, not a Julian date converter.

Worked example: July 21, 2026

TaskJavaC# / .NETResult
Day of year d.getDayOfYear() d.DayOfYear 202
YYYYDDD d.getYear() * 1000 + d.getDayOfYear() d.Year * 1000 + d.DayOfYear 2026202
YYYYDDD to date LocalDate.ofYearDay(c / 1000, c % 1000) new DateTime(c / 1000, 1, 1).AddDays(c % 1000 - 1) 2026-07-21
JDE CYYDDD (d.getYear() - 1900) * 1000 + d.getDayOfYear() (d.Year - 1900) * 1000 + d.DayOfYear 126202
Julian Day Number d.getLong(JulianFields.JULIAN_DAY) d.DayNumber + 1721426 2461243
Modified Julian Date d.getLong(JulianFields.MODIFIED_JULIAN_DAY) d.DayNumber - 678575 61242

Check any value with the Julian date converter, read the Julian Day explainer, or see the same conversions in Python and JavaScript.

Frequently asked questions

How do I get the Julian date in Java?

For the day-of-year form, call getDayOfYear() on a LocalDate for the day number, and format the date with the DateTimeFormatter pattern "uuuuDDD" for YYYYDDD. For the astronomical Julian Day Number, read the JulianFields.JULIAN_DAY field with getLong.

Why does my Java formatter print the wrong year at New Year?

Capital YYYY in a DateTimeFormatter or SimpleDateFormat pattern is the week-based year, which can differ from the calendar year in the last days of December and the first days of January. Use yyyy or uuuu for the year.

How do I get the Julian date in C#?

The DayOfYear property of a DateTime or DateOnly gives the day number, and d.Year * 1000 + d.DayOfYear gives YYYYDDD as an integer. For the Julian Day Number, add 1721426 to the DayNumber of a DateOnly.

Is the .NET JulianCalendar class a Julian date converter?

No. That class is the old Julian calendar of Julius Caesar, with different month dates from today's Gregorian calendar. It does not produce day-of-year codes or astronomical Julian Dates.