Code · Java & C#

Julian dates in Java and C#

How to produce both kinds of "Julian date" in Java and C#/.NET: the ordinal YYYYDDD day-of-year form used in manufacturing and IT, and the continuous astronomical Julian Day (JD) used in astronomy. Both languages have first-class date types, so most of this is one or two lines.

Two meanings of "Julian date"

The ordinal Julian date is a year plus the day-of-year: 2026202 (YYYYDDD) or 26202 (YYDDD). The astronomical Julian Day counts days continuously since noon on 1 January 4713 BC — for example 2461242.5. They are unrelated systems that share a name, so this page keeps them in separate sections.

Java — ordinal day-of-year (YYYYDDD)

Use java.time.LocalDate. getDayOfYear() returns the plain day-of-year, and a DateTimeFormatter pattern with the D letter builds the padded string. In these patterns "DDD" pads the day-of-year to a minimum of three digits.

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

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

int doy = d.getDayOfYear();                 // 202

// YYYYDDD -> "2026202"  ('D' = day of year, "DDD" pads to 3)
String yyyyddd = d.format(DateTimeFormatter.ofPattern("yyyyDDD"));

// YYDDD -> "26202"
String yyddd = d.format(DateTimeFormatter.ofPattern("yyDDD"));

Parsing the ordinal string back to a date uses the same pattern:

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

LocalDate d = LocalDate.parse(
    "2026202", DateTimeFormatter.ofPattern("yyyyDDD"));  // 2026-07-21

Java — astronomical Julian Day (JD / MJD)

Java ships the answer in java.time.temporal.JulianFields: JULIAN_DAY gives the integer Julian Day Number and MODIFIED_JULIAN_DAY gives the MJD. For a fractional JD, use toEpochDay() (days since 1970-01-01) and add 2440587.5, the JD of the Unix epoch at midnight.

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

// Built-ins (java.time.temporal) — the clean, correct way:
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

// Fractional JD by hand, from the epoch day (days since 1970-01-01):
double jdMidnight = d.toEpochDay() + 2440587.5;         // 2461242.5

// With a time-of-day, use the instant:
static double julianDay(Instant t) {
    return t.getEpochSecond() / 86400.0 + 2440587.5;
}
// MJD = JD - 2400000.5

C# (.NET) — ordinal day-of-year (YYYYDDD)

DateTime.DayOfYear gives the day-of-year directly. Unlike Java, .NET's custom date format strings have no day-of-year specifier, so assemble the ordinal string from the year and a zero-padded ("D3") day number.

using System;

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

int doy = d.DayOfYear;                       // 202

// .NET has no day-of-year format specifier, so build the string.
// YYYYDDD -> "2026202"
string yyyyddd = d.ToString("yyyy") + d.DayOfYear.ToString("D3");

// YYDDD -> "26202"
string yyddd = d.ToString("yy") + d.DayOfYear.ToString("D3");

To parse an ordinal string, split it and add the days to 1 January of that year:

using System;

string s   = "2026202";
int year   = int.Parse(s.Substring(0, 4));
int doy    = int.Parse(s.Substring(4));
DateTime d = new DateTime(year, 1, 1).AddDays(doy - 1);   // 2026-07-21

C# (.NET) — astronomical Julian Day (JD / MJD)

ToOADate() is not a Julian Day

DateTime.ToOADate() returns an OLE Automation date (days since 30 December 1899), which is a different epoch entirely. Compute JD from the Unix epoch instead, and be deliberate about DateTimeKind — convert to UTC first so the result isn't shifted by the local time zone.

using System;

// ToOADate() is an OLE Automation date, NOT a Julian Day — don't use it here.
// Compute JD from the Unix epoch instead. Work in UTC.
static double JulianDay(DateTime dt)
{
    DateTime utc   = dt.ToUniversalTime();
    DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    return (utc - epoch).TotalDays + 2440587.5;
}

var d  = new DateTime(2026, 7, 21, 0, 0, 0, DateTimeKind.Utc);
double jd  = JulianDay(d);      // 2461242.5
double mjd = jd - 2400000.5;    // 61242

Worked example — 21 July 2026

21 July 2026 is day-of-year 202, ordinal 2026202, and astronomical JD 2461242.5 at midnight UTC (MJD 61242). Both languages agree.

Task Java C# / .NET
Day of year d.getDayOfYear() d.DayOfYear
Ordinal YYYYDDD format("yyyyDDD") ToString("yyyy") + DayOfYear.ToString("D3")
Julian Day Number getLong(JulianFields.JULIAN_DAY) (utc - epoch).TotalDays + 2440587.5
Modified Julian Day getLong(JulianFields.MODIFIED_JULIAN_DAY) JD - 2400000.5

Check any value with the Julian date converter, read the Julian Day explainer for the astronomical system, or browse the code index for the same conversions in Python and JavaScript.