Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A simple date question

 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I made the following menu to let user input date info and I added to the jsp page.

<%
String year = "";
String month = "";
String day = "";
%>
<td width="300">
<select name="year" id="<%=year%>">
<option selected>2002</option>
<option>2003</option>
<option>2004</option>
<option>2005</option>
<option>2006</option>
</select>
<select name="month" id="<%=month%>">
<option selected>01</option>
<option>02</option>
<option>03</option>
<option>04</option>
<option>05</option>
<option>06</option>
<option>07</option>
<option>08</option>
<option>09</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
<select name="day" id="<%=day%>">
<option selected>01</option>
<option>02</option>
<option>03</option>
<option>04</option>
<option>05</option>
<option>06</option>
<option>07</option>
<option>08</option>
<option>09</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select>
</td>

I have a date attribute in one of the tables of a mysql database. The format is yyyy-mm-dd.
I want to get the year, month and day from the jsp page, combine them to be a date variable in the servlet and input to the mysql db.
How should I combine the year, month and day to be String date?
Here is the servlet:
private String year;
private String month;
private String day;
private String date;
public void setYear(String year)
{
this.year = year;
}
public String getYear()
{
return year;
}
public void setMonth(String month)
{
this.month= month;
}
public String getMonth()
{
return month;
}
public void setDay(String day)
{
this.day = day;
}
public String getDay()
{
return day;
}
public void setDate(String date)
{
this.date = new StringBuffer().append(year).append("-").append(month).append("-").append(day).toString();
}
public String getDate()
{
return date;
}
Anyone could help?
Thanks
Andrew
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the servlet:
That sure doesn't look like a servlet to me. It looks more like the insides of a JavaBean.
You haven't shown us the FORM tag in your HTML example. What are you using as the "action" of your HTML form? A servlet? A JSP? something else ? Before we can help with processing your date input, we need to know what it is being sent to.
 
Byron Estes
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also the string formats for dates accepted within sql vary by dbms. Just take a look at the sql documentaion on your dbms to see, what formats they accept.
Regards,
 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The html form tag is
<form method="post" action="/servlet/com.abc.web.Date">
And the mysql date format is yyyy-mm-dd.
Thanks for your kind help
Andrew
 
ernest fakudze
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew!
I would implement the situation as follows:
a) Have just 1 html textfield for the date and you should instruct your users to input the date in a certain format e.g. yyyymmdd. It's always a good idea to use a string to hold your date in the database because like Byron has said, different DBMS handle dates differently and it becomes a nightmare when you have to migrate databases.
b) In your servlet/jsp use the String.substring() method to extract the year, month and date and then use the Calender class to validate them. Then you can join them together and form a Date object. I'm sorry I'm not being precise here because I'm not showing you the code but I'm sure you will get the idea.
As you can see, this keeps your HTML code shorter.
All the best!!
 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi andrew
even u can use something like this too inside yr serlvet..
String bdate= request.getParameter("year")+"/"+request.getParameter("month")+"/"+request.getParameter("day");
hth..
Malhar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic