001    /**
002     * Copyright 2007 Mike Kroutikov.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     *   it under the terms of the Lesser GNU General Public License as 
006     *   published by the Free Software Foundation; either version 3 of
007     *   the License, or (at your option) any later version.
008     *
009     *   This program is distributed in the hope that it will be useful,
010     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *   Lesser GNU General Public License for more details.
013     *
014     *   You should have received a copy of the Lesser GNU General Public License
015     *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
016     */
017    
018    package org.otfeed.support;
019    
020    import java.text.ParseException;
021    import java.text.SimpleDateFormat;
022    import java.util.Date;
023    
024    /**
025     * Date format helper. Default format is "MM/dd/yyyy HH:mm.ss".
026     * See documantation for <code>java.text.SimpleDateFormat</code> 
027     * for the explanation of possible patterns.
028     */
029    public class DateFormat implements IFormat<Date> {
030            
031            private static final String DEFAULT_FORMAT = "MM/dd/yyyy HH:mm.ss";
032    
033            private SimpleDateFormat format;
034            
035            /**
036             * Creates new DateFormat. The default pattern is
037             * "MM/dd/yyyy HHH:mm.ss"
038             */
039            public DateFormat() {
040                    this(DEFAULT_FORMAT);
041            }
042    
043            /**
044             * Creates new DateFormat from a given pattern.
045             * @param pattern
046             */
047            public DateFormat(String pattern) {
048                    setPattern(pattern);
049            }
050            
051            /**
052             * Determines the date formatting/parsing pattern.
053             * 
054             * @return pattern value.
055             */
056            public String getPattern() { 
057                    return format.toPattern(); 
058            }
059            
060            /**
061             * Sets pattern value.
062             * 
063             * @param pattern
064             */
065            public void setPattern(String pattern) {
066                    format = new SimpleDateFormat(pattern);
067            }
068    
069            /**
070             * Formats Date object to string.
071             */
072            public String format(Date obj) {
073                    return format.format(obj);
074            }
075    
076            /**
077             * Parses Date object from a string.
078             */
079            public Date parse(String val) {
080                    try {
081                            return format.parse(val);
082                    } catch (ParseException e) {
083                            throw new IllegalArgumentException("parsing error: " + e.getMessage());
084                    }
085            }
086    }