0

I have a jquery datepicker on my page :

$( "#dob" ).datepicker({
        yearRange: "-100:+0", 
        dateFormat: 'dd-mm-yyyy',
        changeMonth: true,
        changeYear: true,
        showAnim: 'slideDown',
        minDate: '-100Y',
        maxDate: '-1D'
    });

I am getting dob string as : 25-12-1988 in my servlet.

i tried this :

SimpleDateFormat formatter=new SimpleDateFormat("dd-mm-yyyy");
            try {
                ud.setDob(formatter.parse(dob));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

to insert this in Db i am doing this :

pstm.setDate(9,new java.sql.Date(ud.getDob().getTime()));

and in db i got this : 1988-01-25 00:00:00 which is incorrect.

datatype of column is datetime

where is the problem ?

1 Answer 1

4

mm is "minutes". "Months" is MM:

SimpleDateFormat formatter=new SimpleDateFormat("dd-MM-yyyy");

(See the javadoc for java.text.SimpleDateFormat.)

Sign up to request clarification or add additional context in comments.

3 Comments

Um, are you going to provide the fixed format instead of just the broken one?
can i make sure before parsing that the string is in correct format---- for example if i would get string like this : 25/12/1988 or 25-12-88 then it will through an exception ??
@JAVAGeek: For 25/12/1988, you don't need to do anything special; it will automatically raise an exception. 25-12-88 is trickier, because as far as SimpleDateFormat is concerned, 88 is a valid year. You can either use a regular expression to ensure that it matches \d\d-\d\d-\d\d\d\d (e.g. if(! Pattern.matches("\\d\\d-\\d\\d-\\d\\d\\d\\d", dob)) { throw new Exception(); }), or else check afterward to make sure that the date is within a sensible range. You'll probably also want to call formatter.setLenient(false) so that it's stricter about getting a date that actually exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.