Solved

setting default values to a from field from a java calendar

Posted on 2003-11-04
6
231 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 86
egit plugin on eclipse 8 65
MySQL  on Tomcat 8 47
iterator/ListIterator approach 17 28
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 second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

831 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