You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by ka...@wipro.com on 2011/09/29 08:54:35 UTC

Struts 2 date field problem

I want to get the struts 2 date field as Date,

But it return the string value,

My bean property is date data type any way Is there to directly get the
date data type from struts 2 date tag


Please do not print this email unless it is absolutely necessary. 

The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments. 

WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. 

www.wipro.com

RE: Struts 2 date field problem

Posted by ka...@wipro.com.
Thanks for reply,
My challenge is without converting string to date we can acheive
I am using <s:textfield key="nameOfInputVal" value="%{getText(nameOfInputVal.date,{'nameOfInputVal'})}"  />  
In struts.properties file I set nameOfInputVal.date = {0,date,dd.MM.yyyy}

Buy I can't able to get the value in action class. Any one suggest this way ,please help me
-----Original Message-----
From: Li Ying [mailto:liying.cn.2010@gmail.com] 
Sent: Tuesday, October 11, 2011 6:37 PM
To: Struts Users Mailing List
Subject: Re: Struts 2 date field problem

Did your problem happens when date input or output?

For output case, the <s:date> tag can let you choose the format to use.
See:
http://struts.apache.org/2.x/docs/date.html

For input case, Struts2 can convert HTTP parameters from String into
most of data type you need, include Date.

But for Date, it [uses the SHORT format for the Locale associated with
the current request]

So, if your parameters can not be converted to Date correctly, maybe you need:
(1)change your input format
(2)or build a new Type Converter to support the format you want.

This page describes the details:
http://struts.apache.org/2.x/docs/type-conversion.html

And, the following code is a DateConverter we used in our project,
which support several date formats:

================= START =================
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.util.StrutsTypeConverter;

public class DateConverter extends StrutsTypeConverter {
	private String[] dateFormats = new String[] {
			"yyyy/MM/dd HH:mm:ss",
			"yyyy/MM/dd" };

	@Override
	@SuppressWarnings("rawtypes")
	public Object convertFromString(final Map context, final String[] values,
			final Class toClass) {
		String dateStr = values[0];

		if (StringUtils.isEmpty(dateStr)) {
			return null;
		}

		for (String formatStr : dateFormats) {
			SimpleDateFormat format = new SimpleDateFormat(formatStr);
			try {
				return format.parse(dateStr);
			} catch (ParseException e) {
			}
		}

		throw new RuntimeException("Could not parse date. [" + dateStr + "]");
	}

	@Override
	@SuppressWarnings("rawtypes")
	public String convertToString(final Map context, final Object o) {
		return o.toString();
	}
}
================= END =================




2011/9/29  <ka...@wipro.com>:
> I want to get the struts 2 date field as Date,
>
> But it return the string value,
>
> My bean property is date data type any way Is there to directly get the
> date data type from struts 2 date tag
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Please do not print this email unless it is absolutely necessary. 

The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments. 

WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. 

www.wipro.com

Re: Struts 2 date field problem

Posted by Li Ying <li...@gmail.com>.
Did your problem happens when date input or output?

For output case, the <s:date> tag can let you choose the format to use.
See:
http://struts.apache.org/2.x/docs/date.html

For input case, Struts2 can convert HTTP parameters from String into
most of data type you need, include Date.

But for Date, it [uses the SHORT format for the Locale associated with
the current request]

So, if your parameters can not be converted to Date correctly, maybe you need:
(1)change your input format
(2)or build a new Type Converter to support the format you want.

This page describes the details:
http://struts.apache.org/2.x/docs/type-conversion.html

And, the following code is a DateConverter we used in our project,
which support several date formats:

================= START =================
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.util.StrutsTypeConverter;

public class DateConverter extends StrutsTypeConverter {
	private String[] dateFormats = new String[] {
			"yyyy/MM/dd HH:mm:ss",
			"yyyy/MM/dd" };

	@Override
	@SuppressWarnings("rawtypes")
	public Object convertFromString(final Map context, final String[] values,
			final Class toClass) {
		String dateStr = values[0];

		if (StringUtils.isEmpty(dateStr)) {
			return null;
		}

		for (String formatStr : dateFormats) {
			SimpleDateFormat format = new SimpleDateFormat(formatStr);
			try {
				return format.parse(dateStr);
			} catch (ParseException e) {
			}
		}

		throw new RuntimeException("Could not parse date. [" + dateStr + "]");
	}

	@Override
	@SuppressWarnings("rawtypes")
	public String convertToString(final Map context, final Object o) {
		return o.toString();
	}
}
================= END =================




2011/9/29  <ka...@wipro.com>:
> I want to get the struts 2 date field as Date,
>
> But it return the string value,
>
> My bean property is date data type any way Is there to directly get the
> date data type from struts 2 date tag
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org