Julian day of the year in Java

If all you want is the day-of-year, why don'you just use GregorianCalendars DAY_OF_YEAR field?

import java.util.GregorianCalendar;
public class CalTest {
    public static void main(String[] argv) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.set(GregorianCalendar.DAY_OF_MONTH, 8);
        gc.set(GregorianCalendar.MONTH, GregorianCalendar.JUNE);
        gc.set(GregorianCalendar.YEAR, 2010);
        System.out.println(gc.get(GregorianCalendar.DAY_OF_YEAR));
}

}

Alternatively, you could calculate the difference between today's Julian date and that of Jan 1st of this year. But be sure to add 1 to the result, since Jan 1st is not the zeroth day of the year:

int[] now = {2010, 6, 8};
int[] janFirst = {2010, 1, 1};
double dayOfYear = toJulian(now) - toJulian(janFirst) + 1
System.out.println(Double.valueOf(dayOfYear).intValue());

tl;dr

LocalDate.now().getDayOfYear()

…or…

org.threeten.extra.DayOfYear.now() 

“Julian day” terminology

The term “Julian day” is sometimes used loosely to mean the ordinal day of the year, or Ordinal date, meaning a number from 1 to 365 or 366 (leap years). January 1 is 1, January 2 is 2, December 31 is 365 (or 366 in leap years).

This loose (incorrect) use of Julian probably comes from the use in astronomy and other fields of tracking dates as a continuous count of days since noon Universal Time on January 1, 4713 BCE (on the Julian calendar). Nowadays the Julian date is approaching two and half million, 2,457,576 today.

java.time

The java.time framework built into Java 8 and later provides an easy facility to get the day-of-year.

The LocalDate class represents a date-only value without time-of-day and without time zone. You can interrogate for the day-of-year.

LocalDate localDate = LocalDate.of ( 2010 , Month.JUNE , 8 );
int dayOfYear = localDate.getDayOfYear ();

Dump to console. Results show that June 8, 2010 is indeed day # 159.

System.out.println ( "localDate: " + localDate + " | dayOfYear: " + dayOfYear );

localDate: 2010-06-08 | dayOfYear: 159

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

ZoneId z = ZoneId.of( “America/Montreal” );
LocalDate today = LocalDate.now( z );
int dayOfYear = today.getDayOfYear ();

Going the other direction, from a number to a date.

LocalDate ld = Year.of( 2017 ).atDay( 159 ) ;

org.threeten.extra.DayOfYear

The ThreeTen-Extra library adds functionality to the java.time classes built into Java.

This library offers a class to represent explicitly the ordinal day of any year: DayOfYear. Using this class rather than a mere integer number makes your code more self-documenting, provides type-safety, and ensures valid values.

DayOfYear dayOfYear = DayOfYear.from( LocalDate.of ( 2010 , Month.JUNE , 8 ) ) ;

Get a date for a DayOfYear with specific year.

LocalDate localDate = dayOfYear.atYear( 2023 ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 brought some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android (26+) bundle implementations of the java.time classes.
    • For earlier Android (<26), the process of API desugaring brings a subset of the java.time functionality not originally built into Android.
      • If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.


DateFormat d = new SimpleDateFormat("D");
System.out.println(d.format(date));

import java.util.Calendar;
// ...
final int julianDay = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);

Note that this doesn't take into account the "starts at noon" deal claimed by that weird site you referenced. That could be fixed by just checking the time of course.