Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Compute the day of the year that Easter falls on. Java code example - Click here to copy ->>>

   Can't find what you're looking for? Try our search:

Really working examples categorized by API, package, class. You can compile and run our examples right away! Not from source code for Java projects - only working examples! Copy, compile and run!
------------------

Code:

 
/*
 * Copyright (c) Ian F. Darwin, http://www.darwinsys.com/, 1996-2002.
 * All rights reserved. Software written by Ian F. Darwin and others.
 * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
*/

import java.util.*;
import java.applet.*;

/** Easter - compute the day on which Easter falls.
 *
 * In the Christian religion, Easter is possibly the most important holiday
 * of the year, so getting its date <I>just so</I> is worthwhile.
 *
 * @author: Ian F. Darwin, http://www.darwinsys.com/,
 * based on a detailed algorithm in Knuth, vol 1, pg 155.
 *
 * @Version: $Id: Easter.java,v 1.5 2004/02/09 03:33:46 ian Exp $
 * Written in C, Toronto, 1988. Java version 1996.
 *
 * @Note: It's not proven correct, although it gets the right answer for
 * years around the present.
 */
public class Easter {

   /* Compute the day of the year that Easter falls on.
    * Step names E1 E2 etc., are direct references to Knuth, Vol 1, p 155.
    * @exception IllegalArgumentexception If the year is before 1582 (since the
    *       algorithm only works on the Gregorian calendar).
    */
   public static final Calendar findHolyDay(int year) {
      if (year <= 1582) {
         throw new IllegalArgumentException("Algorithm invalid before April 1583");
      }
      int golden, century, x, z, d, epact, n;

      golden = (year % 19) + 1;   /* E1: metonic cycle */
      century = (year / 100) + 1;   /* E2: e.g. 1984 was in 20th C */
      x = (3*century/4) - 12;      /* E3: leap year correction */
      z = ((8*century+5) / 25) -5;    /* E3: sync with moon's orbit */
      d = (5*year/4) - x - 10;
      epact = (11*golden + 20 + z - x) % 30; /* E5: epact */
      if ((epact == 25 && golden > 11) || epact == 24)
         epact++;
      n = 44 - epact;
      n += 30 * (n < 21?1:0);      /* E6: */
      n += 7 - ((d+n)%7);
      if (n>31)         /* E7: */
         return new GregorianCalendar(year, 4-1, n-31);   /* April */
      else
         return  new GregorianCalendar(year, 3-1, n);   /* March */
   }

   /** Main program, when used as a standalone application */
   public static void main(String[] argv) {

      if (argv.length == 0) {
         int thisYear = new GregorianCalendar().get(Calendar.YEAR);
         Calendar c = Easter.findHolyDay(thisYear);
         System.out.println( c.getTime());
      } else for (int i=0; i<argv.length; i++) {
         int year = 0;
         try {
            year = Integer.parseInt(argv[i]);
            System.out.println(Easter.findHolyDay(year).getTime());
         } catch (IllegalArgumentException e) {
            System.err.println("Year " + argv[i] + " invalid (" + e.getMessage() + ").");
         }
      }
   }
}

 
 



References.

The list of classes which were used on this page you can find below. The links to Java API contain official SUN documentation about all used classes.

  java.util.Calendar

  java.util.GregorianCalendar

  java.lang.IllegalArgumentException




[ Go Back ]



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy