You need to display the date in the format yyyy / mm / DD in Java

I'm using nebulla datechoosercombo I use it as datechooserfrom getText();. It produces results such as 7 / 31 / 2011 in the format of M / DD / yyyy and mm / DD / yyyy I need results in yyyy / mm / DD format To do this, I used code

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
String s = df.format(dateChooserFrom.getText());

However, when I run the code, it says that a given object cannot be formatted as a date So anyone can help me I am using eclipse RCP and Java

Solution

DateFormat dffrom = new SimpleDateFormat("M/dd/yyyy");
DateFormat dffrom = new SimpleDateFormat("M/dd/yyyy");
DateFormat dfto = new SimpleDateFormat("yyyy-MM-dd");  
Date today = dffrom.parse("7/1/2011");
String s = dfto.format(today);

First convert the string to a date

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>