Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* 
   * Copyright (C) 2012 FoxLabs
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   * http://www.apache.org/licenses/LICENSE-2.0
   * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.foxlabs.validation.converter;
 
 import java.util.Map;
 import java.util.Date;
 
 
This class provides Converter implementation for the java.util.Date type.

 
 public class DateConverter extends AbstractConverter<Date> {
    
    
DateConverter default instance.
 
     public static final DateConverter DEFAULT = new DateConverter();
    
    
java.text.SimpleDateFormat date/time pattern.
 
     protected final String pattern;
    
    
java.text.DateFormat date style.
 
     protected final int dateStyle;
    
    
java.text.DateFormat time style.
 
     protected final int timeStyle;
    
    
Constructs default DateConverter.
 
     protected DateConverter() {
         this. = null;
         this. = this. = -1;
     }
    
    
Constructs a new DateConverter with the specified date/time pattern.

Parameters:
pattern java.text.SimpleDateFormat date/time pattern.
Throws:
java.lang.IllegalArgumentException if the specified pattern is null or invalid.
 
     protected DateConverter(String pattern) {
         this. = new SimpleDateFormat(pattern).toPattern();
         this. = this. = -1;
     }
    
    
Constructs a new DateConverter from the specified annotation.

Parameters:
annotation Converter annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines invalid pattern.
 
     protected DateConverter(DatePattern annotation) {
         this(annotation.value());
     }
    
    
Constructs a new DateConverter with the specified date and time styles.

Parameters:
dateStyle java.text.DateFormat date style.
timeStyle java.text.DateFormat time style.
Throws:
java.lang.IllegalArgumentException if the specified date or time style is invalid.
    protected DateConverter(int dateStyleint timeStyle) {
        if (timeStyle < 0) {
            DateFormat.getDateInstance(dateStyle);
        } else if (dateStyle < 0) {
            DateFormat.getTimeInstance(timeStyle);
        } else {
            DateFormat.getDateTimeInstance(dateStyletimeStyle);
        }
        this. = null;
        this. = dateStyle;
        this. = timeStyle;
    }
    
    
Constructs a new DateConverter from the specified annotation.

Parameters:
annotation Converter annotation.
Throws:
java.lang.IllegalArgumentException if the specified annotation defines invalid date/time style.
    protected DateConverter(DateStyle annotation) {
        this(annotation.date(), annotation.time());
    }
    
    
Returns java.util.Date type.

Returns:
java.util.Date type.
    @Override
    public final Class<DategetType() {
        return Date.class;
    }
    
    
In case of localized conversion appends pattern argument that contains localized date/time pattern.

Parameters:
context Validation context.
arguments Arguments to be substituted into the error message template.
Returns:
true.
    @Override
    public boolean appendMessageArguments(ValidationContext<?> contextMap<StringObjectarguments) {
        super.appendMessageArguments(contextarguments);
        if (context.isLocalizedConvert()) {
            DateFormat format = getFormat(context);
            if (format instanceof SimpleDateFormat)
                arguments.put("pattern", ((SimpleDateFormatformat).toLocalizedPattern());
        }
        return true;
    }
    
    
Converts string representation of date/time into java.util.Date object.

Parameters:
value String representation of date/time.
context Validation context.
Returns:
Decoded java.util.Date object.
Throws:
MalformedValueException if the specified string could not be parsed as a date/time.
    @Override
    protected <T> Date doDecode(String valueValidationContext<T> context) {
        if (context.isLocalizedConvert()) {
            ParsePosition pos = new ParsePosition(0);
            Date date = getFormat(context).parse(valuepos);
            if (pos.getIndex() == value.length())
                return date;
        } else {
            try {
                return doDecodeDefault(value);
            } catch (IllegalArgumentException e) {}
        }
        throw new MalformedValueException(thiscontextvalue);
    }
    
    
Converts default string representation of date/time into java.util.Date object.

Parameters:
value Default string representation of date/time.
Returns:
Decoded java.util.Date object.
Throws:
java.lang.IllegalArgumentException if the specified string could not be parsed as a date/time.
    @SuppressWarnings("deprecation")
    protected Date doDecodeDefault(String value) {
        return new Date(value);
    }
    
    
Converts java.util.Date object into string representation.

Parameters:
value java.util.Date object to be encoded.
context Validation context.
Returns:
String representation of date/time.
    @Override
    protected <T> String doEncode(Date valueValidationContext<T> context) {
        return context.isLocalizedConvert() ? getFormat(context).format(value) : doEncodeDefault(value);
    }
    
    
Converts java.util.Date object into default string representation.

Parameters:
value java.util.Date object to be encoded.
Returns:
Default string representation of date/time.
    protected String doEncodeDefault(Date value) {
        return value.toString();
    }
    
    
Returns date/time format for the specified context and this converter configuration.

Parameters:
context Validation context.
Returns:
Date/time format for the specified context and this converter configuration.
    protected DateFormat getFormat(ValidationContext<?> context) {
        return  == null
            ? context.getDateFormat()
            : context.getDateFormat();
    }
    
New to GrepCode? Check out our FAQ X