469,883 Members | 1,231 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,883 developers. It's quick & easy.

Calendar in java ??

Hi, I have to reuse the code given below for displaying a calendar according to the month and year specified by user ...currently this code is fetching systme date and genrating calendar for that... my question is how to pass the variable month and year to this java code to get calendar for that year


Expand|Select|Wrap|Line Numbers
  1.  package com.cj.htmlcal;
  2.  
  3. import java.text.SimpleDateFormat;
  4. import java.util.*;
  5.  
  6. public class HtmlCalendar
  7. {
  8.  
  9.     private static final String VERSION = "ver. 1.6";
  10.  
  11.     private static String NEWLINE = "\n";
  12.     public static final int MONDAY_FIRST = 1;
  13.     public static final int SUNDAY_FIRST = 2;
  14.     private Hashtable cnf;
  15.     private static final String Mnth[] = {
  16.         "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
  17.         "Nov", "Dec"
  18.     };
  19.     private int year;
  20.     private int month;
  21.     private int style;
  22.     private String sFont;
  23.     private Locale loc;
  24.  
  25.     public HtmlCalendar()
  26.     {
  27.         sFont = null;
  28.         GregorianCalendar gregoriancalendar = new GregorianCalendar();
  29.         cnf = new Hashtable();
  30.         NEWLINE = System.getProperty("line.separator");
  31.         style = 2;
  32.         month = gregoriancalendar.get(2) + 1;
  33.         year = gregoriancalendar.get(1);
  34.         loc = Locale.US;
  35.     }
  36.  
  37.     public void setLocale(Locale locale)
  38.     {
  39.         loc = locale;
  40.     }
  41.  
  42.     public Locale getLocale()
  43.     {
  44.         return loc;
  45.     }
  46.  
  47.     public void setYear(int i)
  48.     {
  49.         if(i > 0)
  50.         {
  51.             year = i;
  52.             cnf.clear();
  53.         }
  54.     }
  55.  
  56.     public int getYear()
  57.     {
  58.         return year;
  59.     }
  60.  
  61.     public void setMonth(int i)
  62.     {
  63.         if(i >= 1 && i <= 12)
  64.         {
  65.             month = i;
  66.             cnf.clear();
  67.         }
  68.     }
  69.  
  70.     public int getMonth()
  71.     {
  72.         return month;
  73.     }
  74.  
  75.     public void setStyle(int i)
  76.     {
  77.         style = i;
  78.     }
  79.  
  80.     public int getStyle()
  81.     {
  82.         return style;
  83.     }
  84.  
  85.     private int getDay(Calendar calendar)
  86.     {
  87.         if(style == 2)
  88.             return calendar.get(7) - 1;
  89.         else
  90.             return (calendar.get(7) + 5) % 7;
  91.     }
  92.  
  93.     public void setActions(String s, String s1)
  94.     {
  95.         for(int i = 1; i <= 31; i++)
  96.             setAction(i, s, s1);
  97.  
  98.     }
  99.  
  100.     public void setAction(int i, String s, String s1)
  101.     {
  102.         if(s != null)
  103.         {
  104.             cnf.put("" + i, s);
  105.             if(s1 != null && s1.length() > 0)
  106.                 cnf.put(i + "target", s1);
  107.         }
  108.     }
  109.  
  110.     public String getHtml()
  111.     {
  112.         GregorianCalendar gregoriancalendar = new GregorianCalendar(year, month - 1, 1);
  113.         GregorianCalendar gregoriancalendar1 = new GregorianCalendar(2001, 0, 14);
  114.         SimpleDateFormat simpledateformat = new SimpleDateFormat("EEE", loc);
  115.         int i = month - 1;
  116.         int j = 0;
  117.         StringBuffer stringbuffer = new StringBuffer("");
  118.         stringbuffer.append("<table>\n");
  119.         if(style == 2)
  120.         {
  121.             stringbuffer.append("<th align=right>" + formatObject(sFont, simpledateformat.format(gregoriancalendar1.getTime())) + "\n");
  122.             gregoriancalendar1.add(5, 1);
  123.             for(int k = 1; k < 7; k++)
  124.             {
  125.                 stringbuffer.append("<th align=right>" + formatObject(sFont, simpledateformat.format(gregoriancalendar1.getTime())) + "\n");
  126.                 gregoriancalendar1.add(5, 1);
  127.             }
  128.  
  129.         } else
  130.         {
  131.             gregoriancalendar1.add(5, 1);
  132.             stringbuffer.append("<th align=right>" + formatObject(sFont, simpledateformat.format(gregoriancalendar1.getTime())) + "\n");
  133.             for(int l = 2; l < 8; l++)
  134.             {
  135.                 gregoriancalendar1.add(5, 1);
  136.                 stringbuffer.append("<th align=right>" + formatObject(sFont, simpledateformat.format(gregoriancalendar1.getTime())) + "\n");
  137.             }
  138.  
  139.         }
  140.         stringbuffer.append("</tr>\n");
  141.         int i1 = 0;
  142.         j = 0;
  143.         if(getDay(gregoriancalendar) > 0)
  144.         {
  145.             stringbuffer.append("<tr>");
  146.             for(; i1 < getDay(gregoriancalendar); i1++)
  147.             {
  148.                 stringbuffer.append("<td align=right>");
  149.                 if(sFont != null)
  150.                     stringbuffer.append(sFont + " </font>");
  151.                 else
  152.                     stringbuffer.append(" ");
  153.                 stringbuffer.append("</td>\n");
  154.                 j++;
  155.             }
  156.  
  157.         }
  158.         for(; gregoriancalendar.get(2) == i; gregoriancalendar.add(5, 1))
  159.         {
  160.             int j1 = gregoriancalendar.get(5);
  161.             int k1 = (i1 + j1) % 7;
  162.            /*if(k1 == 1)
  163.             {
  164.                 stringbuffer.append("<tr>" + NEWLINE);
  165.                 j = 0;
  166.             }*/
  167.             stringbuffer.append("<td align=right>");
  168.             j++;
  169.             if(sFont != null)
  170.                 stringbuffer.append(sFont);
  171.             String s;
  172.             if((s = (String)cnf.get(j1 + "")) != null)
  173.             {
  174.                 stringbuffer.append("<a href=\"");
  175.                 if(s.toUpperCase().startsWith("HTT") || s.indexOf(".") > 0)
  176.                 {
  177.                     stringbuffer.append(s);
  178.                     if(s.indexOf("?") < 0)
  179.  
  180.  
  181.                 stringbuffer.append("?date=" + stringDate(gregoriancalendar));
  182.                 else
  183.                  stringbuffer.append("&date=" + stringDate(gregoriancalendar));
  184.  
  185.                 }
  186.                 else
  187.                 {
  188.                       stringbuffer.append("javascript:" + s + "('" + stringDate(gregoriancalendar) + "');");
  189.                 }
  190.                 stringbuffer.append("\"");
  191.                 if((s = (String)cnf.get(j1 + "target")) != null)
  192.                     stringbuffer.append(" target=\"" + s + "\"");
  193.                 stringbuffer.append(">");
  194.                 stringbuffer.append(gregoriancalendar.get(5));
  195.                 stringbuffer.append("</a>\n");
  196.             } else
  197.             {
  198.                 stringbuffer.append(j1 + "");
  199.             }
  200.             if(sFont != null)
  201.                 stringbuffer.append("</font>");
  202.            //stringbuffer.append("</td>\n");
  203.             if(k1 == 0)
  204.                 stringbuffer.append("</tr>\n");
  205.         }
  206.  
  207.         if(j < 7)
  208.         {
  209.             for(; j < 7; j++)
  210.             {
  211.                 stringbuffer.append("<td align=right>");
  212.                 if(sFont != null)
  213.                     stringbuffer.append(sFont);
  214.                 stringbuffer.append(" ");
  215.                 if(sFont != null)
  216.                     stringbuffer.append("</font>");
  217.                 stringbuffer.append("</td>\n");
  218.             }
  219.  
  220.             stringbuffer.append("</tr>\n");
  221.         }
  222.         stringbuffer.append("</table>\n");
  223.         return stringbuffer.toString();
  224.     }
  225.  
  226.     private String stringDate(Calendar calendar)
  227.     {
  228.         String s = "" + calendar.get(1);
  229.         //return s + twoDigits(calendar.get(2) + 1) + twoDigits(calendar.get(5));
  230.         return s + twoDigits(calendar.get(2) + 1);
  231.     }
  232.  
  233.     private String twoDigits(int i)
  234.     {
  235.         String s = "" + i;
  236.         if(s.length() == 1)
  237.             return "0" + s;
  238.         else
  239.             return s;
  240.     }
  241.  
  242.     private String formatObject(String s, Object obj)
  243.     {
  244.         String s1 = "";
  245.         if(obj != null)
  246.             s1 = "" + obj;
  247.         if(s == null)
  248.             return s1;
  249.         else
  250.             return s + s1 + "</font>";
  251.     }
  252.  
  253. }
Feb 24 '08 #1
0 1394

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by vigi98 | last post: by
2 posts views Thread by cg_news | last post: by
4 posts views Thread by chennakeshava_ramesh | last post: by
1 post views Thread by COmbined | last post: by
19 posts views Thread by skyblue | last post: by
1 post views Thread by Larry C | last post: by
4 posts views Thread by abhishekbrave | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.