An introduction to the JulianDay Utility

April 11, 2002 -- The Julian Day utility provides routines that are useful when performing astronomical ephemeris computations or simple date arithmetic. The Julian Day or Julian Day Number is the continuous count of days since the year -4712. The JulianDay class implements the algorithms presented by Jean Meeus, Astronomical Algorithms, Willmann-Bell, Inc., 1998, pp 59-66. The following code example shows how to use JulianDay class to compute the Julian Day Number for a given date, compute the Gregorian calendar date for a given Julian Day Number, perform simple date arithmetic, calculate day of week from Julian Day Number, and use the utility to format a date for output.

The source code may be downloaded from JulianDay.zip. Simply unzip the file in your favorite directory and review the JulianDayExample.java located in the src/com/raben/examples directory. The JavaDoc for the JulianDay class and the source cross reference are available. If you have installed ant on your system, you can can compile the source and example by changing to the src directory and entering the command:
ant all
.

To run the example, change to the dist directory and enter the command:
java -jar JulianDay.jar.

/*
 * JulianDayExample.java
 *
 * Created on March 29, 2002, 12:57 PM
 */

package com.raben.example;
import com.raben.util.JulianDay;
/**
 *
 * @author  Vern Raben
 * @version 1.0
 */
public final class JulianDayExample {
    
  public static void main(String[] args) {
    /**
      * To get the Julian Day for the current 
      * time, just call the constructor with
      * no arguments
      */
    JulianDay julianDay=new JulianDay();
      
    // Print out the current JDN
    System.out.println("Current jdn is "+julianDay.getJDN());
      
    // Calculate the Julian Day one week ago
    julianDay.add(JulianDay.DATE,-7);
    System.out.println("JulianDay one week ago was "
        +julianDay.getJDN());
      
    // Calculate Julian Day when Sputnik-1 was 
    // launched (Oct 4, 1957 at 19:26 UTC)
    JulianDay sputJD=new JulianDay(1957,JulianDay.OCTOBER,4,19,26);     
    System.out.println("Sputnik-1 was launched at JD "
        +sputJD.getJDN());
    
    // What day of the week was it (0=SUNDAY,1=MONDAY,etc)
    System.out.println("Day of week was "
        +sputJD.get(JulianDay.DAY_OF_WEEK));
    
    /**
     * You may also specify the output date format according to 
     * java.text.SimpleDateFormat
     * Note that this only works for dates after Jan 1970, prior to that
     * the date will be printed in sql date format by default
     * Also, the date is always UTC
     */
    julianDay.setDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("Today is "+julianDay.getDateTimeStr()+" UTC");
    
    }

}
 

Back to Article index