text: :
  Beginning Java
  Holiday in date
  Kristol Crawley
  Greenhorn
  Posts: 7
  posted 16 years ago
  Is it possible to tell if the date is a holiday? I looked through the api for calendar (we're on 1.3.1 btw) and I didn't see anything that looked like it would return that.
  Jason Menard
  Sheriff
  Posts: 6450
  posted 16 years ago
  Java
  does not provide this functionality automatically. It will require some work on your part. Check out JavaWorld's Tips 'N Tricks
  Java Tip 44: Calculating holidays and their observances
  for some more info.
  Kristol Crawley
  Greenhorn
  Posts: 7
  posted 16 years ago
  I figured it'd be messy!
  Thank you Jason!
  Michael Dunn
  Ranch Hand
  Posts: 4632
  posted 16 years ago
  here's one I was working on some time ago (I'm not from the US so I'm not entirely sure I have the parameters of the holidays correct)
  easter calculation is close, but is incorrect (this is when I stopped), you might be able to add a tweak or two, to get it right.
  import java.util.*; import java.text.*; class Holidays { public static Calendar cal = Calendar.getInstance(); public static String holidays[][] = new String [13][32]; public static DateFormat df = new SimpleDateFormat( "EEEE, d MMMM yyyy" ); public static void main(String args[]) { int year = Integer.parseInt(args[0]); final String data[][] = {{"0","1","1","New Year's Day"},{"3","2","1","Martin Luther King Jr Day"}, {"3","2","2","President's Day"},{"5","2","5","Memorial Day"}, {"0","4","7","Independence Day"},{"1","2","9","Labor Day"}, {"2","2","10","Columbus Day"},{"0","11","11","Veteran's Day"}, {"4","5","11","Thansgiving Day"},{"0","25","12","Christmas Day"}}; for(int i = 0; i < data.length; i++) { setHolidayDate(Integer.parseInt(data[i][0]), Integer.parseInt(data[i][1]), Integer.parseInt(data[i][2]), year, data[i][3]); } setEaster(year); holidays[0][0] = holidays[12][31]; /*for 1 Jan falling on saturday*/ holidays[12][31] = ""; /*ditto */ for(int i = 0; i < 13; i++) { for(int ii = 0; ii < 32; ii++) { if(holidays[i][ii] != null) System.out.println(holidays[i][ii]); } } } public static void setHolidayDate(int week, int day, int month, int year, String name) { int dayOfWeek; if(week == 0) /*it's a day of the month eg 25 December*/ { cal.set(year,month - 1,day); dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); if(dayOfWeek == 7) cal.set(year,month - 1,day - 1); else if(dayOfWeek == 1) cal.set(year,month - 1,day + 1); } else if(week < 5)/*it's a day of the week, in the month eg 3rd Monday in January*/ { cal.set(year, month - 1, 1); dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); if(dayOfWeek <= day) cal.set(year, month - 1, 1 - dayOfWeek + day + ((week - 1) * 7)); else cal.set(year, month - 1, 1 - dayOfWeek + day + (week * 7)); } else /*a day in the last week of the month*/ { /*set up as 5th week eg last monday in may*/ cal.set(year, month - 1, 1); dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); do { day += 7; cal.set(year, month - 1, 1 - dayOfWeek + day); }while(cal.get(Calendar.MONTH) == month - 1); cal.set(year, month - 1, 1 - dayOfWeek + day - 7); } /* set the holiday date*/ holidays[cal.get(Calendar.MONTH) + 1] [cal.get(Calendar.DAY_OF_MONTH)] = (String)df.format(cal.getTime()) + " " + name; } public static void setEaster(int year) { int y = ((int)((18.37 * (year % 19)) - 6 + .5)) % 29; int x = ((year - 1900) * 365 + (int) (year - 1900)/4 + 59 + y + 1) % 7; if(x == 0) x = 7; cal.set(year, 2, y - x + 29 - 2); /* good friday*/ holidays[cal.get(Calendar.MONTH) + 1] [cal.get(Calendar.DAY_OF_MONTH)] = (String)df.format(cal.getTime()) + " " + "Good Friday"; cal.set(year, 2, y - x + 29 + 1); /* easter monday*/ holidays[cal.get(Calendar.MONTH) + 1] [cal.get(Calendar.DAY_OF_MONTH)] = (String)df.format(cal.getTime()) + " " + "Easter Monday"; } }
  Don't get me started about those stupid
  light bulbs
  .
  reply
  Bookmark Topic
  Watch Topic
  New Topic
  Boost this thread!
  Similar Threads
  Can I get the date of a specific weekday with the weeknumber as attribute?
  Problem with Calendar().getTime()
  Could my program fail with race condition by concurrent threads in a multi thread environment?
  Calculate next Business day excluding weekend and Holidays
  Comparing Dates, How To?
  More...