Solved

setting default values to a from field from a java calendar

Posted on 2003-11-04
6
226 Views
Last Modified: 2010-03-31
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()">
      </form>
0
Comment
Question by:bobwilli
  • 2
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 84 total points
ID: 9681392
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()) ;
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 84 total points
ID: 9681440
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
0
 
LVL 92

Assisted Solution

by:objects
objects earned 41 total points
ID: 9681639
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()">
     </form>

0
 
LVL 92

Expert Comment

by:objects
ID: 11430895
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now