aspose file tools*
The moose likes Struts and the fly likes converting strings to dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "converting strings to dates" Watch "converting strings to dates" New topic
Author

converting strings to dates

Nicola Guy
Ranch Hand

Joined: Jun 23, 2004
Posts: 91
Hi

heres the problem I have a string that holds a date in the format YYYYMMDD, and this cannot be changed.

However when i want to display it using struts I need a way to change it to YYYY-MM-DD using the bean:write tag because I have get the string from an element

any quick help would be appreciated
Sudeep Victor
Greenhorn

Joined: Jan 10, 2003
Posts: 13
Hi nicola,

I'm assuming that the date is an attribute in a bean and the bean has a getter and a setter.now in your getter method u could format your string date DDMMYYYY into a string DD/MM/YYYY and return it .The bean:write when calling the getter method now can display the date in the required format.

Regards,
Sudeep
here is a sample code to convert date from one format to the other.You can probably put collection of such code in a util class and call that in your bean.

public static String toClientDate(String cServerString)
{
Date oDateObject = toDate(cServerString, SERVER_DATE);
String cClientString = toString(oDateObject, CLIENT_DATE);
return(cClientString);
}

// Convert a free date/time into a date object.

public static Date toDate(String cDateTimeString, String cDateTimeFormat){
SimpleDateFormat oDateTimeFormat = new SimpleDateFormatcDateTimeFormat);
Date oDateObject = null;
try {
oDateObject=oDateTimeFormat.parse(cDateTimeString,new ParsePosition0));
}catch (Exception oException) { oDateObject = null; }
if (oDateObject == null) oDateObject = new Date(0);
return(oDateObject);
}

// Convert a date object into a free date/time.
public static String toString(Date oDateObject, String cDateTimeFormat)
{
SimpleDateFormat oDateTimeFormat = new SimpleDateFormat(cDateTimeFormat);
String cDateTimeString = oDateTimeFormat.format(oDateObject);
return(cDateTimeString);
}
Nicola Guy
Ranch Hand

Joined: Jun 23, 2004
Posts: 91
i cant do that because the string date is passed on via XML and has to remain a string of YYYYMMDD format any other ideas?
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Originally posted by nicola guy:
Hi

heres the problem I have a string that holds a date in the format YYYYMMDD, and this cannot be changed.

However when i want to display it using struts I need a way to change it to YYYY-MM-DD using the bean:write tag because I have get the string from an element

any quick help would be appreciated


Use JSTL. You could store your output from <bean:write> into a variable using <c:set>, next format this variable using <fmt>. You'd need to take a look at JSTL's API for more information.

Cheers!


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: converting strings to dates