Calendar range

In need to print range of customized calendar date date strings, I wrote this simple java program. Here is how it looks highlighted by Code2HTML jEdit plugin. Information about date string formatis for example at java site.

   1:/**
   2: * By Josef Chlachula, March 2009 
   3: */
   4:package apod;
   5:
   6://import java.lang.Integer;
   7:import java.util.Calendar;
   8:import java.util.Date;
   9:import java.util.GregorianCalendar;
  10:import java.text.FieldPosition;
  11:import java.text.SimpleDateFormat;
  12:
  13:/**
  14: * This program prints dates from start date to end date
  15: */
  16:public class CalRange {
  17:
  18: public static void help(String msg){
  19:   if (msg != null) System.out.println();
  20:   System.out.println("Usage:");
  21:   System.out.println("java apod.CalRange yyyy mm dd    DD YYYY MM DD [format string]");
  22:   System.out.println("Examples:");
  23:   System.out.println("java apod.CalRange 1999 12 30     2000 1 5 \"yyyy.MM.dd EEE\"");
  24:   System.out.println("java apod.CalRange 1999 4 1     2001 12 31 \"'Beautifull ' EEEE M/d/yyyy', isn''t it?'\"");
  25:   System.out.println("See date patterns at http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html");
  26: }
  27: public static void main(String[] args) throws java.lang.NumberFormatException {
  28:   if (args.length < 6) {
  29:     help("Not enough arguments.");
  30:     System.exit(1);
  31:   }
  32:   int y1 = Integer.parseInt(args[0]);
  33:   int m1 = Integer.parseInt(args[1]) - 1;
  34:   int d1 = Integer.parseInt(args[2]);
  35:   GregorianCalendar cal = new GregorianCalendar(y1, m1, d1);
  36:   int y2 = Integer.parseInt(args[3]);
  37:   int m2 = Integer.parseInt(args[4]) - 1;
  38:   int d2 = Integer.parseInt(args[5]);
  39:   GregorianCalendar cal2 = new GregorianCalendar(y2, m2, d2);
  40:   SimpleDateFormat dateFormat = null;
  41:   if (args.length > 6) dateFormat = new SimpleDateFormat(args[6]);
  42:   else dateFormat = new SimpleDateFormat();
  43:
  44:   while (cal.before(cal2)){
  45:     Date date = cal.getTime();
  46:     StringBuffer sb = new StringBuffer();
  47:     FieldPosition pos = new FieldPosition(0);
  48:
  49:     String s = dateFormat.format(date, sb, pos).toString();
  50:     System.out.println(s);
  51:     cal.add(Calendar.DAY_OF_MONTH,1);
  52:   }
  53: }
  54:
  55:}
This entry was posted in workday. Bookmark the permalink.

Leave a Reply