The best way to handle dates globally in your application is to use a
TypeConverter by extending
ognl.DefaultTypeConverter;
and overriding
public Object convertValue(Map map, Object object, Class aClass).
You register your type converter in xwork-conversion.properties
java.util.Date=com.myapp.converters.DateConverter
In the example below it will accept may inputs from the user but will
always print out the first pattern (MM/dd/yyyy). Of course you can
have it print what ever format you want when going from Date to
String. There are other examples that use Calendars types to capture
date and time.
-D
public class DateConverter extends DefaultTypeConverter {
Log log = LogFactory.getLog(DateConverter.class);
public Object convertValue(Map map, Object object, Class aClass) {
/
***********************************************************Set
Standard Format*/
String[] parsePatterns = {"MM/dd/yyyy", "dd-MMM-yyyy",
"MM.dd.yyyy", "mm/dd/yy"};
FastDateFormat df =
FastDateFormat.getInstance(parsePatterns[1]);
if (aClass == Date.class) {
/
********************************************************Get First
Value in Parameters Array*/
String source = ((String[]) object)[0];
Date transfer;
try {
transfer = DateUtils.parseDate(source, parsePatterns);
return transfer;
} catch (ParseException e) {
throw new RuntimeException("Cannot convert " + source
+ " to calendar type");
}
} else if (aClass == String.class) {
Date o = (Date) object;
return df.format(o);
}
return null;
}
}
On Aug 22, 2008, at 3:31 AM, Hardik Shah wrote:
hi
i am using hibernate 3 with s2 my problem is that
im mysql table i have field of date type
so i have set type date in hb mapping.xml and in pojo(if i am set
as string
in pojo and mapping file it gives error )
now i have to set date in input as format mm/dd/yy so mysql
accepts it
automatically and convert in its foramt type(YY-dd-mm) otherwise it
return
to input result
but i want to show it as dd/mm/yy ,what i have to do for this any
convertor
of s2 apply or what?
please help to solve this problem
-----
Java/J2EE developer
India
blogs
http://hardik4u.wordpress.com wordpress blog
--
View this message in context:
http://www.nabble.com/struts-hibernate-mysql-date-problem-tp19104863p19104863.html
Sent from the Struts - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]