aspose file tools*
The moose likes Servlets and the fly likes A simple date question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "A simple date question" Watch "A simple date question" New topic
Author

A simple date question

Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
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

Joined: Jan 07, 1999
Posts: 6920
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.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Byron Estes
Ranch Hand

Joined: Feb 21, 2002
Posts: 313
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,


Byron Estes<br />Sun Certified Enterprise Architect<br />Senior Consulant<br />Blackwell Consulting Services<br />Chicago, IL<br /><a href="http://www.bcsinc.com" target="_blank" rel="nofollow">www.bcsinc.com</a>
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
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

Joined: Aug 27, 2001
Posts: 216
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!!


In a time of drastic change it is the learners who inherit the future. The learned usually find themselves equipped to live in a world that no longer exists.<br />Eric Hoffer
Malhar Barai
Author
Ranch Hand

Joined: Aug 17, 2001
Posts: 399
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A simple date question