473,320 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Calendar in java

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

No i am new to java.. the working of this current code is that when we call this java calss from jsp page it displays dates of the current month.. i want to use it for month and year ented bu user.
Mar 1 '08 #3
Please explain your problem clearly.
Mar 2 '08 #4
Please explain your problem clearly.
Currently I have two files 1 is a jaa bean class file poted in the question and the other is jsp file which is calling the java bean class.

Currently when I execute this code it genrates calendar of current month and year . It takes the current month and year from system.
But I want to modify tis code in a way so that I can pass values of month and year from a jsp to this java file and then this java file will genrate calendar of that particular year.
Mar 3 '08 #5

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

Similar topics

4
by: vigi98 | last post by:
Hello all, I do the following : SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar d = new GregorianCalendar(2004,11,30); d.add(Calendar.DATE, 7); dt=d.getTime();...
2
by: cg_news | last post by:
In short, what I am trying to do is, based on a date, calculate the week of year (as described in ISO 8601), then calculate the first and last date in this week period and return them in the format...
4
by: chennakeshava_ramesh | last post by:
hi, I have a problem, I am not able to find out which day of the week it is using the calendar class. I am using set() function to set the date and want to find out which day i.e mon,tue etc of...
1
by: COmbined | last post by:
Does anyone know of a control (freeware or commercial) that has a swing based implementation of a calendar control that I can use for recording bookings for objects Thanks Combined
19
by: skyblue | last post by:
//can somebody help me with this please. i need to create a //calendar on the frame. i got the frame but the calendar is not //woking. import jpb.*; import java.awt.*; import...
0
by: Rajkishore Kumar | last post by:
We have a piece of code written in Java Script in an ASP.Net appln in Visual Studio 2003 and it works fine. What it does is , when you press a date icon , it would open up a calendar(prepared by...
1
by: Larry C | last post by:
Hello, Not a programmer at all so I apologize if this makes no sense. We have an old app that runs on Windows Server 2003 and uses OC4J. We wanted to add a calendar to the app that we could...
1
by: basm101 | last post by:
Hi, I have a java Calendar object, observationDate. java.util.Calendar observationDate=subsequentSub.getObservationDate(); I want to trigger a javascript function upon clicking a radio...
0
by: abhishekbrave | last post by:
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......
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.