Dinup Kandel Dinup Kandel - 6 months ago 30
Java Question

need to show date in yyyy/mm/dd format in java

I am using

Nebulla DateChooserCombo
. I have used it as
dateChooserFrom.getText();
. It is Producing the result like 7/31/2011 which is in
m/dd/yyyy
and
mm/dd/yyyy
format. I need the result in
yyyy/mm/dd
format. For that I have use the code as.

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


But when I run the code it says
Cannot format given Object as a Date
. so please anybody could help me on this. I am using eclipse rcp and java.

Answer
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);

Convert the String to Date first.

Comments