문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top