0
public LigneReservation(Date dateArrivee, Date dateDepart,
        String categorie, int quantite) {
    super();

    SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd");
    try {
        this.dateArrivee = form.parse(form.format(dateArrivee));
        this.dateDepart = form.parse(form.format(dateDepart));
        String s = form.format(dateArrivee);

        System.out.print(form.parse(s));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    this.categorie = categorie;
    this.quantite = quantite;
}

This is how I call the method:

ctrlRes.setLigneCourante(ctrlRes.creerLigne(dateArrivee.getDate(),
    dateDepart.getDate(), (String)listeCatCh.getSelectedItem(),
    Integer.parseInt(champQteCh.getText())));

So I first extract the date from JDateChooser fields and then I pass them to the constructor LigneReservation, the String S is showing me the correct format I want "yyyy-mm-dd" but when I parse it to a date, it gives me dates like this: Wed Mar 13 00:00:00 EDT 2013. How can I correct it ?

Thanks

didierc
  • 14,572
  • 3
  • 32
  • 52
user2133558
  • 524
  • 2
  • 10
  • 29

1 Answers1

2

but when i parse it to a date, it gives me dates like this: Wed Mar 13 00:00:00 EDT 2013.

When you parse it, you get a Date. Using Date.toString() always gives you the same format.

Converts this Date object to a String of the form:

dow mon dd hh:mm:ss zzz yyyy

If you want to format a Date in a particular format, use SimpleDateFormat instead.

(Alternatively, use Joda Time and its parsing/formatter facilities, which are generally nicer - as is the rest of the API.)

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • But that's what i used, problem is SimpleDateFormat .format returns a String while i want a date, so that's why i am parsing the returned String, or did i misunderstand you ? – user2133558 Mar 10 '13 at 20:45