You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by jp...@mchsi.com on 2009/07/07 19:44:21 UTC

Converter Problem

I am having issues with a custom component that I created. The component's function is to ensure that the date entered adheres to the format "^(\\d{2})/(\\d{2})/(\\d{4})$" The problem, though, is when the form that contains the component is submitted, the date is converted to the previous day's date. For example, if a date of 07/07/2009 is entered, the value that gets submitted is 07/06/2009 at 19:00 CDT. My code (2 classes) is as follows:

public class CustomDateTextField extends TextField {

    public CustomDateTextField(String id, IModel model, Class type) {
        super(id, model, type);
        add(new EzdecDatePicker());
    }

    public CustomDateTextField(String id, IModel model) {
        super(id, model);
        add(new EzdecDatePicker());
    }

    public CustomDateTextField(String id, Class type) {
        super(id, type);
        add(new EzdecDatePicker());
    }

    public CustomDateTextField(String id) {
        super(id);
        add(new EzdecDatePicker());
    }

    @Override
    public IConverter getConverter(Class<?> type) {
        return new StrictPatternDateConverter();
    }
}

public class StrictPatternDateConverter extends PatternDateConverter {

    public static final String REGEX_PATTERN =
            "^(\\d{2})/(\\d{2})/(\\d{4})$";

    public StrictPatternDateConverter() {
        super("MM/dd/yyyy", false);
    }

    @Override
    public Date convertToObject(String value, Locale locale) {
        final Pattern pattern = Pattern.compile(REGEX_PATTERN);
        if (StringUtils.isNotBlank(value) && !pattern.matcher(value).matches()) {
            throw new ConversionException("Invalid date format");
        }
        return super.convertToObject(value, locale);
    }
}

Re: Converter Problem

Posted by jpalmer1026 <jp...@mchsi.com>.
For what it's worth, I added the following Calendar code to the end of the
method, which fixed the problem, but I'm thinking there has to be a better
way to handle this.

public Date convertToObject(String value, Locale locale) {
        final Pattern pattern = Pattern.compile(REGEX_PATTERN);
        if (StringUtils.isNotBlank(value) &&
!pattern.matcher(value).matches()) {
            throw new ConversionException("Invalid date format");
        }
        Date d = super.convertToObject(value, locale);
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.add(Calendar.HOUR, 8);
        c.set(Calendar.HOUR, 0);
        return c.getTime();
    }


jpalmer1026 wrote:
> 
> 
> The DateTime I was referring to is in the
> org.apache.wicket.datetime.DateConverter class, so I have no way of
> modifying it. Any other ideas?
> 
> 
> Stijn Maller wrote:
>> 
>> Looking at your datepattern you should be using LocalDate instead of
>> DateTime, as you don't have a time or a timezone anyway. IMVHO this
>> should
>> solve all your problems.
>> 
> 
> -- 
> View this message in context:
> http://www.nabble.com/Converter-Problem-tp24378227p24392993.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Converter-Problem-tp24378227p24399351.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Converter Problem

Posted by jpalmer1026 <jp...@mchsi.com>.
The DateTime I was referring to is in the
org.apache.wicket.datetime.DateConverter class, so I have no way of
modifying it. Any other ideas?


Stijn Maller wrote:
> 
> Looking at your datepattern you should be using LocalDate instead of
> DateTime, as you don't have a time or a timezone anyway. IMVHO this should
> solve all your problems.
> 

-- 
View this message in context: http://www.nabble.com/Converter-Problem-tp24378227p24392993.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Converter Problem

Posted by jpalmer1026 <jp...@mchsi.com>.
The DateTime I was referring to is in the
org.apache.wicket.datetime.DateConverter class, so I have no way of
modifying it. Any other ideas?


Stijn Maller wrote:
> 
> Looking at your datepattern you should be using LocalDate instead of
> DateTime, as you don't have a time or a timezone anyway. IMVHO this should
> solve all your problems.
> 

-- 
View this message in context: http://www.nabble.com/Converter-Problem-tp24378227p24392990.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Converter Problem

Posted by Stijn Maller <st...@gmail.com>.
Looking at your datepattern you should be using LocalDate instead of
DateTime, as you don't have a time or a timezone anyway. IMVHO this should
solve all your problems.
-- 
View this message in context: http://www.nabble.com/Converter-Problem-tp24378227p24391721.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Converter Problem

Posted by Stefan Malmesjö <s....@gmail.com>.
The "format.parseDateTime" can do some tricky stuff, it seems:

"Parses a datetime from the given text, returning a new DateTime.

The parse will use the zone and chronology specified on this formatter.

If the text contains a time zone string then that will be taken into 
account in adjusting the time of day as follows. If the 
||withOffsetParsed() 
<http://grepcode.com/file/repo1.maven.org$maven2@joda-time$joda-time@1.6@org$joda$time$format$DateTimeFormatter.java#DateTimeFormatter.withOffsetParsed%28%29>|| 
has been called, then the resulting DateTime will have a fixed offset 
based on the parsed time zone. Otherwise the resulting DateTime will 
have the zone of this formatter, but the parsed zone may have caused the 
time to be adjusted."

Can you debug step into that and see if anything funky happens there?

/Stefan



On 2009-07-07 22:47, jpalmer1026 wrote:
> I did a little bit of digging and it appears that the problem is in the
> convertToObject() in the org.apache.wicket.datetime.DateConverter class.
> Specifically, the return date.ToDate() line in the following code:
>
> if (applyTimeZoneDifference)
> 		{
> 			TimeZone zone = getClientTimeZone();
> 			...
> }
> else
> 		{
> 			try
> 			{
> 				DateTime date = format.parseDateTime(value);
> 				return date.toDate();
> 			}
> }
>
>
> jpalmer1026 wrote:
>    
>> I was thinking the same thing but, as you pointed out, I explicitly set it
>> to false, so I'm not sure why it's behaving this way. I tried setting it
>> to true just as an experiment but the results were the same. Has anyone
>> else seen this?
>>
>>
>> Stefan Malmesjö wrote:
>>      
>>> Could this have anything to do with applyTimeZoneDifference in
>>> PatterDateConverter? It looks like you set it to false, so it shouldn't,
>>> but still... it fits the description pretty well...
>>> /Stefan
>>>
>>> On 2009-07-07 19:44, jpalmer1026@mchsi.com wrote:
>>>        
>>>> I am having issues with a custom component that I created. The
>>>> component's function is to ensure that the date entered adheres to the
>>>> format "^(\\d{2})/(\\d{2})/(\\d{4})$" The problem, though, is when the
>>>> form that contains the component is submitted, the date is converted
>>>> to the previous day's date. For example, if a date of 07/07/2009 is
>>>> entered, the value that gets submitted is 07/06/2009 at 19:00 CDT. My
>>>> code (2 classes) is as follows:
>>>>
>>>> public class CustomDateTextField extends TextField {
>>>>
>>>>      public CustomDateTextField(String id, IModel model, Class type) {
>>>>          super(id, model, type);
>>>>          add(new EzdecDatePicker());
>>>>      }
>>>>
>>>>      public CustomDateTextField(String id, IModel model) {
>>>>          super(id, model);
>>>>          add(new EzdecDatePicker());
>>>>      }
>>>>
>>>>      public CustomDateTextField(String id, Class type) {
>>>>          super(id, type);
>>>>          add(new EzdecDatePicker());
>>>>      }
>>>>
>>>>      public CustomDateTextField(String id) {
>>>>          super(id);
>>>>          add(new EzdecDatePicker());
>>>>      }
>>>>
>>>>      @Override
>>>>      public IConverter getConverter(Class<?>  type) {
>>>>          return new StrictPatternDateConverter();
>>>>      }
>>>> }
>>>>
>>>> public class StrictPatternDateConverter extends PatternDateConverter {
>>>>
>>>>      public static final String REGEX_PATTERN =
>>>>              "^(\\d{2})/(\\d{2})/(\\d{4})$";
>>>>
>>>>      public StrictPatternDateConverter() {
>>>>          super("MM/dd/yyyy", false);
>>>>      }
>>>>
>>>>      @Override
>>>>      public Date convertToObject(String value, Locale locale) {
>>>>          final Pattern pattern = Pattern.compile(REGEX_PATTERN);
>>>>          if (StringUtils.isNotBlank(value)&&
>>>> !pattern.matcher(value).matches()) {
>>>>              throw new ConversionException("Invalid date format");
>>>>          }
>>>>          return super.convertToObject(value, locale);
>>>>      }
>>>> }
>>>>          
>>>
>>>        
>>      
>
>    


Re: Converter Problem

Posted by jpalmer1026 <jp...@mchsi.com>.
I did a little bit of digging and it appears that the problem is in the
convertToObject() in the org.apache.wicket.datetime.DateConverter class.
Specifically, the return date.ToDate() line in the following code:

if (applyTimeZoneDifference)
		{
			TimeZone zone = getClientTimeZone();
			...
}
else
		{
			try
			{
				DateTime date = format.parseDateTime(value);
				return date.toDate();
			}
}


jpalmer1026 wrote:
> 
> I was thinking the same thing but, as you pointed out, I explicitly set it
> to false, so I'm not sure why it's behaving this way. I tried setting it
> to true just as an experiment but the results were the same. Has anyone
> else seen this?
> 
> 
> Stefan Malmesjö wrote:
>> 
>> Could this have anything to do with applyTimeZoneDifference in 
>> PatterDateConverter? It looks like you set it to false, so it shouldn't, 
>> but still... it fits the description pretty well...
>> /Stefan
>> 
>> On 2009-07-07 19:44, jpalmer1026@mchsi.com wrote:
>>> I am having issues with a custom component that I created. The 
>>> component's function is to ensure that the date entered adheres to the 
>>> format "^(\\d{2})/(\\d{2})/(\\d{4})$" The problem, though, is when the 
>>> form that contains the component is submitted, the date is converted 
>>> to the previous day's date. For example, if a date of 07/07/2009 is 
>>> entered, the value that gets submitted is 07/06/2009 at 19:00 CDT. My 
>>> code (2 classes) is as follows:
>>>
>>> public class CustomDateTextField extends TextField {
>>>
>>>     public CustomDateTextField(String id, IModel model, Class type) {
>>>         super(id, model, type);
>>>         add(new EzdecDatePicker());
>>>     }
>>>
>>>     public CustomDateTextField(String id, IModel model) {
>>>         super(id, model);
>>>         add(new EzdecDatePicker());
>>>     }
>>>
>>>     public CustomDateTextField(String id, Class type) {
>>>         super(id, type);
>>>         add(new EzdecDatePicker());
>>>     }
>>>
>>>     public CustomDateTextField(String id) {
>>>         super(id);
>>>         add(new EzdecDatePicker());
>>>     }
>>>
>>>     @Override
>>>     public IConverter getConverter(Class<?> type) {
>>>         return new StrictPatternDateConverter();
>>>     }
>>> }
>>>
>>> public class StrictPatternDateConverter extends PatternDateConverter {
>>>
>>>     public static final String REGEX_PATTERN =
>>>             "^(\\d{2})/(\\d{2})/(\\d{4})$";
>>>
>>>     public StrictPatternDateConverter() {
>>>         super("MM/dd/yyyy", false);
>>>     }
>>>
>>>     @Override
>>>     public Date convertToObject(String value, Locale locale) {
>>>         final Pattern pattern = Pattern.compile(REGEX_PATTERN);
>>>         if (StringUtils.isNotBlank(value) && 
>>> !pattern.matcher(value).matches()) {
>>>             throw new ConversionException("Invalid date format");
>>>         }
>>>         return super.convertToObject(value, locale);
>>>     }
>>> }
>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Converter-Problem-tp24378227p24381041.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Converter Problem

Posted by jpalmer1026 <jp...@mchsi.com>.
I was thinking the same thing but, as you pointed out, I explicitly set it to
false, so I'm not sure why it's behaving this way. I tried setting it to
true just as an experiment but the results were the same. Has anyone else
seen this?


Stefan Malmesjö wrote:
> 
> Could this have anything to do with applyTimeZoneDifference in 
> PatterDateConverter? It looks like you set it to false, so it shouldn't, 
> but still... it fits the description pretty well...
> /Stefan
> 
> On 2009-07-07 19:44, jpalmer1026@mchsi.com wrote:
>> I am having issues with a custom component that I created. The 
>> component's function is to ensure that the date entered adheres to the 
>> format "^(\\d{2})/(\\d{2})/(\\d{4})$" The problem, though, is when the 
>> form that contains the component is submitted, the date is converted 
>> to the previous day's date. For example, if a date of 07/07/2009 is 
>> entered, the value that gets submitted is 07/06/2009 at 19:00 CDT. My 
>> code (2 classes) is as follows:
>>
>> public class CustomDateTextField extends TextField {
>>
>>     public CustomDateTextField(String id, IModel model, Class type) {
>>         super(id, model, type);
>>         add(new EzdecDatePicker());
>>     }
>>
>>     public CustomDateTextField(String id, IModel model) {
>>         super(id, model);
>>         add(new EzdecDatePicker());
>>     }
>>
>>     public CustomDateTextField(String id, Class type) {
>>         super(id, type);
>>         add(new EzdecDatePicker());
>>     }
>>
>>     public CustomDateTextField(String id) {
>>         super(id);
>>         add(new EzdecDatePicker());
>>     }
>>
>>     @Override
>>     public IConverter getConverter(Class<?> type) {
>>         return new StrictPatternDateConverter();
>>     }
>> }
>>
>> public class StrictPatternDateConverter extends PatternDateConverter {
>>
>>     public static final String REGEX_PATTERN =
>>             "^(\\d{2})/(\\d{2})/(\\d{4})$";
>>
>>     public StrictPatternDateConverter() {
>>         super("MM/dd/yyyy", false);
>>     }
>>
>>     @Override
>>     public Date convertToObject(String value, Locale locale) {
>>         final Pattern pattern = Pattern.compile(REGEX_PATTERN);
>>         if (StringUtils.isNotBlank(value) && 
>> !pattern.matcher(value).matches()) {
>>             throw new ConversionException("Invalid date format");
>>         }
>>         return super.convertToObject(value, locale);
>>     }
>> }
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Converter-Problem-tp24378227p24378982.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Converter Problem

Posted by Stefan Malmesjö <s....@gmail.com>.
Could this have anything to do with applyTimeZoneDifference in 
PatterDateConverter? It looks like you set it to false, so it shouldn't, 
but still... it fits the description pretty well...
/Stefan

On 2009-07-07 19:44, jpalmer1026@mchsi.com wrote:
> I am having issues with a custom component that I created. The 
> component's function is to ensure that the date entered adheres to the 
> format "^(\\d{2})/(\\d{2})/(\\d{4})$" The problem, though, is when the 
> form that contains the component is submitted, the date is converted 
> to the previous day's date. For example, if a date of 07/07/2009 is 
> entered, the value that gets submitted is 07/06/2009 at 19:00 CDT. My 
> code (2 classes) is as follows:
>
> public class CustomDateTextField extends TextField {
>
>     public CustomDateTextField(String id, IModel model, Class type) {
>         super(id, model, type);
>         add(new EzdecDatePicker());
>     }
>
>     public CustomDateTextField(String id, IModel model) {
>         super(id, model);
>         add(new EzdecDatePicker());
>     }
>
>     public CustomDateTextField(String id, Class type) {
>         super(id, type);
>         add(new EzdecDatePicker());
>     }
>
>     public CustomDateTextField(String id) {
>         super(id);
>         add(new EzdecDatePicker());
>     }
>
>     @Override
>     public IConverter getConverter(Class<?> type) {
>         return new StrictPatternDateConverter();
>     }
> }
>
> public class StrictPatternDateConverter extends PatternDateConverter {
>
>     public static final String REGEX_PATTERN =
>             "^(\\d{2})/(\\d{2})/(\\d{4})$";
>
>     public StrictPatternDateConverter() {
>         super("MM/dd/yyyy", false);
>     }
>
>     @Override
>     public Date convertToObject(String value, Locale locale) {
>         final Pattern pattern = Pattern.compile(REGEX_PATTERN);
>         if (StringUtils.isNotBlank(value) && 
> !pattern.matcher(value).matches()) {
>             throw new ConversionException("Invalid date format");
>         }
>         return super.convertToObject(value, locale);
>     }
> }