mercredi 29 juin 2016

SimpleDateFormat doesn't give me the pattern I need

I'm having a problem with SimpleDateFormat. I have inputfields in my HTML-page with type = date. I want to save these values into my database where they should be saved as dates. I already figured out how to do this and it works. The only probem I encouter is the way the dates are represented in the database.

I want them to be representated as dd-MM-yyyy. Let's say I want to display 01-06-2016 into my database. When doing this with the code I have..it gives me 0006-12-07. Strangely..when I change my pattern into yyyy-MM-dd...it does give me exactly what the pattern says: 2016-06-01. But it doesn't work the other way round.

String parameter = request.getParameter("instroomdatum");
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
try {
    Date parsed = sdf.parse(parameter);
    java.sql.Date sql = new java.sql.Date(parsed.getTime());
    student.setInstroomdatum(sql);
} catch (ParseException ex) {
    Logger.getLogger(StudentController.class.getName()).log(Level.SEVERE, null, ex);
}

Can somebody please explain me what's going on and how to fix this problem? It's really annoying.

EDIT: I tried printing the value of parameter with a simple PrintWriter-object for the specific date 01-06-2016. It shows me 2016-06-01, even though my pattern in the code above is set to dd-MM-yyyy.

Aucun commentaire:

Enregistrer un commentaire