setting default values to a from field from a java calendar

hi I have the below code.

1. I need to set the start and end date out of these code as the value for my form field.

as per this code, rightnow it's brining out the whole "java.util.GregorianCalendar[time=1068969600000,areFieldsSet=true, etc., etc., as the value.

what is wrong.

2. Also besides the default values the form fields should be able to accept user input values and pass them to a sql query.

3. how do you make a query out of these values to get data out of a MYSQL db, where it has the expires date along with other data.


Calendar rightNow = Calendar.getInstance();
System.out.println("startdate" + rightNow) ;
// shift 2 weeks and back to Sunday
Calendar start = Calendar.getInstance();
start.add(Calendar.DATE, 14);
start.set(Calendar.HOUR, 0);
start.set(Calendar.MINUTE, 0);
start.set(Calendar.SECOND, 0);
start.set(Calendar.MILLISECOND, 0);
while(start.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
start.add(Calendar.DATE, -1);

// shift 2 weeks and up to Saturday (following Sundat midnight, actually)
Calendar end = Calendar.getInstance();

end.add(Calendar.DATE, 14);
end.set(Calendar.HOUR, 0);
end.set(Calendar.MINUTE, 0);
end.set(Calendar.SECOND, 0);
end.set(Calendar.MILLISECOND, 0);
while(end.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
end.add(Calendar.DATE, 1);

<form action = ".jsp" method = "post">
Expires between
         <input type = 'text' name ='x' value='<%= start %>' > and
         <input type = text name =y value='<%= end %>'>
      <input type="BUTTON" value="Go" onclick="submit()">
Who is Participating?
CEHJConnect With a Mentor Commented:
1. Because you're printing the whole Calendar:

System.out.println("startdate" + rightNow) ;

where you should (maybe) just print the date

System.out.println("startdate" + rightNow.getTime()) ;
CEHJConnect With a Mentor Commented:
2. You can't use the String value of a Calendar for your form fields. You should probably use separate fields for day, month and year
3. Construct a Date using a SimpleDateFormat using the entered form fields and use that Date as input to a query
objectsConnect With a Mentor Commented:
Use DateFormat to format your date as required.

DataFormat df = new SimpleDateFormat(requiredformat);

<form action = ".jsp" method = "post">
Expires between
        <input type = 'text' name ='x' value='<%= df.format(start.getTime()) %>' > and
        <input type = text name =y value='<%= df.format(end.getTime()) %>'>
     <input type="BUTTON" value="Go" onclick="submit()">

formatting the input parameter value is a suitable (and desirable) solution to the problem.
The default Date format suggested by CEHJ is not really suitable for user input.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.