JSP and the Calendar class


Web tier: servlets, JSP, Web frameworks: JSP and the Calendar class

  1. JSP and the Calendar class (2 messages)


      Are there any good examples on how to handle Dates correctly? I had several "issues" with dates at the moment, and I'm "hoping" there is some easy way to handle it all that I have overlooked.

    1) Whats the best way to accepts dates from a form on a JSP page? At the moment, I am using an <input type="text"> tag and then manually converting it into a CCYYMMDD format and then converting it AGAIN into a DD-MMM-CCYY format to save it to my database (Oracle).

    2) Also, how do you compare two dates with the Calendar class? I tried the equals method, put found that it compares the Object, not the actual date :(

    Thanks in advance for any and all help :)


    Threaded Messages (2)

  2. JSP and the Calendar class[ Go to top ]

    java.util.Date newDate = new Date();// = 9/14/2001
    java.util.Date oldDate = new Date();// = 9/14/2001

    Calendar cal = Calendar.getInstance();
    cal.setTime(oldDate);// = 9/14/2001
    cal.add(cal.MONTH, -2);//takes two months off
    //cal now equals 7/14/2001

    if (cal.before(newDate)) {
       it's true man

    if (cal.after(newDate)) {
       it's false man
  3. JSP and the Calendar class[ Go to top ]

    You might take a close look at the SimpleDateFromat class in java.text package. Its patterns is most probably what you need.