I’m using struts2.x with jquery date picker.
I want to dispaly in the format dd/mm/yyyy. But from the database when fetching it is coming like yyyy-mm-dd.Then i converted it into the required format but the type is String. So next i Converted it to Date type. But the format is changed.
Date getRiskCommDate()
{
String fString = null;
System.out.println("Coming Date from DB"+riskCommDate);
if(riskCommDate!=null)
{
SimpleDateFormat format = new SimpleDateFormat();
format.applyPattern("dd/MM/yyyy");
fString = format.format(riskCommDate);
}
System.out.println("Formated Date in String Form "+fString);
Date d = new Date(fString);
System.out.println("Formated Date in Date form Date "+d);
return d;
}
Output :
Coming Date from DB 2012-07-04
Formated Date in String Form 04/07/2012
Formated Date in Date form Date Sat Apr 07 00:00:00 IST 2012
Any idea is highly appreciated.
The
java.util.Dateclass does not have any property to hold a format.To display the value of the
Datein a custom format, call theformatmethod onSimpleDateFormat.When you do this:
…you are simply calling the
toString()on theDate. That method returns the date in fixed format inStringversion.Based on the comments
If you anyhow wants the custom format with date instance then you can either create a custom class wrapping date instance
or
extendDate