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.
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.
I don't know what Nebulla DateChooserCombo is but I guess the problem is related to the returning type of the instance method getText. Is is a date? Because you have to pass an instance of java.util.Date to the format method.
Use this:
public class Class1 {
public static void main(String args[]) {
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd");//you can use any format that you want, for example:("yyyy/MM/dd")
String s = SDF.format(date);
System.out.print(s);
}
}
精彩评论