Solved

setting default values to a from field from a java calendar

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java 8 lambda expresssions exception handling 3 75
servlet doXXX methods 3 38
MySqlDump not dumping triggers 1 19
Error in @AspectJ Based AOP with Spring 2 4
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

867 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

19 Experts available now in Live!

Get 1:1 Help Now