Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julian day of the year in Java

Tags:

I have seen the "solution" at http://www.rgagnon.com/javadetails/java-0506.html, but it doesn't work correctly. E.g. yesterday (June 8) should have been 159, but it said it was 245.

So, does someone have a solution in Java for getting the current date's three digit Julian day (not Julian date - I need the day this year)?

Thanks! Mark

like image 821
Mark Avatar asked Jun 09 '10 12:06

Mark


People also ask

How do you calculate Julian day of the year?

Here's how to do it: 1) Express the date as Y M D, where Y is the year, M is the month number (Jan = 1, Feb = 2, etc.), and D is the day in the month. 2) If the month is January or February, subtract 1 from the year to get a new Y, and add 12 to the month to get a new M.

What is the Julian date for 2022?

Today's date is 22-10-2022. Today's Julian Date is 2459875.

What is Julian Day format?

Julian date. This format of date is a combination of year plus a relative day number within the year, which is more correctly called an ordinal date. A typical example is 2013-348 in the format YYYYDDD. This is equivalent to a calendar date of December 14 th 2013.


2 Answers

DateFormat d = new SimpleDateFormat("D"); System.out.println(d.format(date)); 
like image 139
mariami Avatar answered Oct 05 '22 23:10

mariami


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.

like image 20
Basil Bourque Avatar answered Oct 05 '22 23:10

Basil Bourque