0

Am facing problem while using @Temporal(TemporalType.DATE) recently we started migrating our project from struts2 to spring while we are using @Temporal(TemporalType.DATE) in struts2 for a java.util.Date field able to retrieve the Date field data as 10/7/14 but coming to springs the same Date field data is retrieving as Tue Oct 07 11:21:00 IST 2014

So,why spring is unable to convert the Tue Oct 07 11:21:00 IST 2014 to 10/7/14 which is a java.util.Date object internally.

How we can achieve this conversion of Tue Oct 07 11:21:00 IST 2014 to 10/7/14 as a java.util.Date Object.

Any help is greatly appreciated thanks.

2 Answers2

2

The format "Tue Oct 07 11:21:00 IST 2014" is the default format if you dump a java.util.Date to string. It has no actual string representation apart from this.

If you want to stringify your date in a particular format, look at SimpleDateFormat.

nablex
  • 4,635
  • 4
  • 36
  • 51
1

You should take a look at SimpleDateFormat.

How to format?

String str="Tue Oct 07 11:21:00 IST 2014";
Date date=new SimpleDateFormat("EEE MMM dd HH:mm:ssZ yyyy").parse(str);
System.out.println(date);
//now you can format this date to any format
//eg
DateFormat df=new SimpleDateFormat("MM/d/yy");
System.out.println(df.format(date));

Out put:

Tue Oct 07 11:21:00 IST 2014
10/7/14
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • Actually it was the perfect solution but i don't want 10/7/14 as a string output i just want 10/7/14 as java.util.Date object.Is there any other solution that you can come up with. – user3763620 Oct 08 '14 at 05:58