Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.util.converter;
027
028import java.text.DateFormat;
029import java.text.ParseException;
030import java.text.SimpleDateFormat;
031import java.util.Date;
032import java.util.Locale;
033import javafx.util.StringConverter;
034
035/**
036 * <p>{@link StringConverter} implementation for {@link Date} values that
037 * represent a date and time.</p>
038 * 
039 * @see DateStringConverter
040 * @see TimeStringConverter
041 */
042public class DateTimeStringConverter extends StringConverter<Date> {
043    
044    // ------------------------------------------------------ Private properties
045    protected final Locale locale;
046    protected final String pattern;
047    protected final DateFormat dateFormat;
048    
049    
050    // ------------------------------------------------------------ Constructors
051    public DateTimeStringConverter() {
052        this(Locale.getDefault());
053    }
054    
055    public DateTimeStringConverter(Locale locale) {
056        this(locale, null);
057    }
058    
059    public DateTimeStringConverter(String pattern) {
060        this(Locale.getDefault(), pattern, null);
061    }
062    
063    public DateTimeStringConverter(Locale locale, String pattern) {
064        this(locale, pattern, null);
065    }
066    
067    public DateTimeStringConverter(DateFormat dateFormat) {
068        this(null, null, dateFormat);
069    }
070    
071    DateTimeStringConverter(Locale locale, String pattern, DateFormat dateFormat) {
072        this.locale = locale;
073        this.pattern = pattern;
074        this.dateFormat = dateFormat;
075    }
076    
077
078    // ------------------------------------------------------- Converter Methods
079
080    /** {@inheritDoc} */
081    @Override public Date fromString(String value) {
082        try {
083            // If the specified value is null or zero-length, return null
084            if (value == null) {
085                return (null);
086            }
087
088            value = value.trim();
089
090            if (value.length() < 1) {
091                return (null);
092            }
093
094            // Create and configure the parser to be used
095            DateFormat parser = getDateFormat();
096
097            // Perform the requested parsing
098            return parser.parse(value);
099        } catch (ParseException ex) {
100            throw new RuntimeException(ex);
101        }
102    }
103
104    /** {@inheritDoc} */
105    @Override public String toString(Date value) {
106        // If the specified value is null, return a zero-length String
107        if (value == null) {
108            return "";
109        }
110
111        // Create and configure the formatter to be used
112        DateFormat formatter = getDateFormat();
113
114        // Perform the requested formatting
115        return formatter.format(value);
116    }
117
118    // --------------------------------------------------------- Private Methods
119
120    /**
121     * <p>Return a <code>DateFormat</code> instance to use for formatting
122     * and parsing in this {@link StringConverter}.</p>
123     */
124    protected DateFormat getDateFormat() {
125        Locale _locale = locale == null ? Locale.getDefault() : locale;
126
127        DateFormat df = null;
128
129        if (dateFormat != null) {
130            return dateFormat;
131        } else if (pattern != null) {
132            df = new SimpleDateFormat(pattern, _locale);
133        } else {
134            df = DateFormat.getDateTimeInstance();
135        }
136
137        df.setLenient(false);
138
139        return df;
140    }
141}