How do I format JDateChooser into dd-mm-yyyy?

juiceb0xk picture juiceb0xk · Dec 2, 2016 · Viewed 7.9k times · Source

I am trying to output the current date format into:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");, but it is outputting like this:

Fri Dec 02 14:03:59 AEST 2016

Here is my code:

JDateChooser datePurchased = new JDateChooser();
DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
Date newDate = new Date();
datePurchased.setDate(newDate);

I am now printing the result like this:

System.out.println(newDate.toString());

But this does not print out what I want, as per above.

My goal output is: 02/12/2016, how do I go about doing this, I've tried looking around but I cannot find the likes to solve my problem.

Thank you in advance.

Answer

ΦXocę 웃 Пepeúpa ツ picture ΦXocę 웃 Пepeúpa ツ · Dec 2, 2016

You are printing the date but no using the formatter, you need to do:

String pattern  = "dd-MM-yyyy";
DateFormat formatter = new SimpleDateFormat(pattern);
System.out.println(formatter.format(newDate));

edit:

if your goal output is: 02/12/2016 then in the pattern in the format incorrect, you will need to use slash and not hyphens

use instead dd/mm/yyyy