001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.dataformat.bindy.format;
018    
019    import java.text.DateFormat;
020    import java.text.SimpleDateFormat;
021    import java.util.Date;
022    import java.util.Locale;
023    
024    import org.apache.camel.dataformat.bindy.PatternFormat;
025    import org.apache.camel.util.ObjectHelper;
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    
029    public class DatePatternFormat implements PatternFormat<Date> {
030    
031        private static final transient Log LOG = LogFactory.getLog(DatePatternFormat.class);
032    
033        private String pattern;
034    
035        public DatePatternFormat() {
036        }
037    
038        public DatePatternFormat(String pattern) {
039            this.pattern = pattern;
040        }
041    
042        public String format(Date object) throws Exception {
043            ObjectHelper.notNull(this.pattern, "pattern");
044            return this.getDateFormat().format(object);
045        }
046    
047        public Date parse(String string) throws Exception {
048    
049            Date date;
050            DateFormat df = this.getDateFormat();
051    
052            ObjectHelper.notNull(this.pattern, "pattern");
053    
054            // Check length of the string with date pattern
055            // To avoid to parse a string date : 20090901-10:32:30 when
056            // the pattern is yyyyMMdd
057    
058            if (string.length() <= this.pattern.length()) {
059    
060                // Force the parser to be strict in the syntax of the date to be
061                // converted
062                df.setLenient(false);
063                date = df.parse(string);
064    
065                return date;
066    
067            } else {
068                throw new FormatException("Date provided does not fit the pattern defined");
069            }
070    
071        }
072    
073        protected java.text.DateFormat getDateFormat() {
074            return new SimpleDateFormat(this.pattern, Locale.FRANCE);
075        }
076    
077        public String getPattern() {
078            return pattern;
079        }
080    
081        /**
082         * Sets the pattern
083         * 
084         * @param pattern the pattern
085         */
086        public void setPattern(String pattern) {
087            this.pattern = pattern;
088        }
089    
090    }