Question

I'm using JComboCalendar widget to let the user select a date. JComboCalendar has a method getDate() which returns a Date object.

According to Oracle docs, Date.toString should return a string with the format yyyy-mm-dd, but instead I'm receiving strings like:

Fri Nov 08 13:08:38 CET 2013

How can I get the expected output? Bonus for how to format it like dd-mm-yyyy, since I just found how to read a String in a given format, not how to format the output.

Was it helpful?

Solution

Use the following to format your date:

new SimpleDateFormat("dd-MM-yyyy").format(comboCalendar.getDate())

As to your other question, you opened the wrong documentation. getDate() returns a java.util.Date not a java.sql.Date Correct documentation: http://docs.oracle.com/javase/7/docs/api/java/util/Date.html#toString%28%29

OTHER TIPS

try this

SimpleDateFormat d1 = new SimpleDateFormat("yyyy-MM-dd");
    Date d=new Date();
    System.out.println(d1.format(d));

output 2013-11-08

SimpleDateFormat will help you.

 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
 String str = sdf.format(yourDateInstance);

Try this out :

SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatDate.format(new Date()); // Sample date to be formatted
System.out.println(formattedDate); // Watch the output
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top