1 /*
   2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.util.converter;
  27 
  28 import java.text.DateFormat;
  29 import java.text.ParseException;
  30 import java.text.SimpleDateFormat;
  31 import java.util.Date;
  32 import java.util.Locale;
  33 import javafx.util.StringConverter;
  34 
  35 /**
  36  * <p>{@link StringConverter} implementation for {@link Date} values that
  37  * represent a date and time.</p>
  38  *
  39  * @see DateStringConverter
  40  * @see TimeStringConverter
  41  * @since JavaFX 2.1
  42  */
  43 public class DateTimeStringConverter extends StringConverter<Date> {
  44 
  45     // ------------------------------------------------------ Private properties
  46     protected final Locale locale;
  47     protected final String pattern;
  48     protected final DateFormat dateFormat;
  49 
  50     /**
  51      * @since JavaFX 8u40
  52      */
  53     protected final int dateStyle;
  54 
  55     /**
  56      * @since JavaFX 8u40
  57      */
  58     protected final int timeStyle;
  59 
  60 
  61     // ------------------------------------------------------------ Constructors
  62 
  63     /**
  64      * Create a {@link StringConverter} for {@link Date} values, using
  65      * {@link DateFormat#DEFAULT} styles for date and time.
  66      */
  67     public DateTimeStringConverter() {
  68         this(null, null, null, DateFormat.DEFAULT, DateFormat.DEFAULT);
  69     }
  70 
  71     /**
  72      * Create a {@link StringConverter} for {@link Date} values, using specified
  73      * {@link DateFormat} styles for date and time.
  74      *
  75      * @param dateStyle the given formatting style. For example,
  76      * {@link DateFormat#SHORT} for "M/d/yy" in the US locale.
  77      * @param timeStyle the given formatting style. For example,
  78      * {@link DateFormat#SHORT} for "h:mm a" in the US locale.
  79      *
  80      * @since JavaFX 8u40
  81      */
  82     public DateTimeStringConverter(int dateStyle, int timeStyle) {
  83         this(null, null, null, dateStyle, timeStyle);
  84     }
  85 
  86     /**
  87      * Create a {@link StringConverter} for {@link Date} values, using the
  88      * specified locale and {@link DateFormat#DEFAULT} styles for date and time.
  89      *
  90      * @param locale the given locale.
  91      */
  92     public DateTimeStringConverter(Locale locale) {
  93         this(locale, null, null, DateFormat.DEFAULT, DateFormat.DEFAULT);
  94     }
  95 
  96     /**
  97      * Create a {@link StringConverter} for {@link Date} values, using specified
  98      * locale and {@link DateFormat} styles for date and time.
  99      *
 100      * @param locale the given locale.
 101      * @param dateStyle the given formatting style. For example,
 102      * {@link DateFormat#SHORT} for "M/d/yy" in the US locale.
 103      * @param timeStyle the given formatting style. For example,
 104      * {@link DateFormat#SHORT} for "h:mm a" in the US locale.
 105      *
 106      * @since JavaFX 8u40
 107      */
 108     public DateTimeStringConverter(Locale locale, int dateStyle, int timeStyle) {
 109         this(locale, null, null, dateStyle, timeStyle);
 110     }
 111 
 112     /**
 113      * Create a {@link StringConverter} for {@link Date} values, using the
 114      * specified pattern.
 115      *
 116      * @param pattern the pattern describing the date and time format.
 117      */
 118     public DateTimeStringConverter(String pattern) {
 119         this(null, pattern, null, DateFormat.DEFAULT, DateFormat.DEFAULT);
 120     }
 121 
 122     /**
 123      * Create a {@link StringConverter} for {@link Date} values, using the
 124      * specified locale and pattern.
 125      *
 126      * @param locale the given locale.
 127      * @param pattern the pattern describing the date and time format.
 128      */
 129     public DateTimeStringConverter(Locale locale, String pattern) {
 130         this(locale, pattern, null, DateFormat.DEFAULT, DateFormat.DEFAULT);
 131     }
 132 
 133     /**
 134      * Create a {@link StringConverter} for {@link Date} values, using the
 135      * specified {@link DateFormat} formatter.
 136      *
 137      * @param dateFormat the {@link DateFormat} to be used for formatting and
 138      * parsing.
 139      */
 140     public DateTimeStringConverter(DateFormat dateFormat) {
 141         this(null, null, dateFormat, DateFormat.DEFAULT, DateFormat.DEFAULT);
 142     }
 143 
 144     DateTimeStringConverter(Locale locale, String pattern, DateFormat dateFormat,
 145                             int dateStyle, int timeStyle) {
 146         this.locale = (locale != null) ? locale : Locale.getDefault(Locale.Category.FORMAT);
 147         this.pattern = pattern;
 148         this.dateFormat = dateFormat;
 149         this.dateStyle = dateStyle;
 150         this.timeStyle = timeStyle;
 151     }
 152 
 153 
 154     // ------------------------------------------------------- Converter Methods
 155 
 156     /** {@inheritDoc} */
 157     @Override public Date fromString(String value) {
 158         try {
 159             // If the specified value is null or zero-length, return null
 160             if (value == null) {
 161                 return (null);
 162             }
 163 
 164             value = value.trim();
 165 
 166             if (value.length() < 1) {
 167                 return (null);
 168             }
 169 
 170             // Create and configure the parser to be used
 171             DateFormat parser = getDateFormat();
 172 
 173             // Perform the requested parsing
 174             return parser.parse(value);
 175         } catch (ParseException ex) {
 176             throw new RuntimeException(ex);
 177         }
 178     }
 179 
 180     /** {@inheritDoc} */
 181     @Override public String toString(Date value) {
 182         // If the specified value is null, return a zero-length String
 183         if (value == null) {
 184             return "";
 185         }
 186 
 187         // Create and configure the formatter to be used
 188         DateFormat formatter = getDateFormat();
 189 
 190         // Perform the requested formatting
 191         return formatter.format(value);
 192     }
 193 
 194     // --------------------------------------------------------- Private Methods
 195 
 196     /**
 197      * <p>Return a <code>DateFormat</code> instance to use for formatting
 198      * and parsing in this {@link StringConverter}.</p>
 199      *
 200      * @return A {@code DateFormat} instance for formatting and parsing in this
 201      * {@link StringConverter}
 202      */
 203     protected DateFormat getDateFormat() {
 204         DateFormat df = null;
 205 
 206         if (dateFormat != null) {
 207             return dateFormat;
 208         } else if (pattern != null) {
 209             df = new SimpleDateFormat(pattern, locale);
 210         } else {
 211             df = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
 212         }
 213 
 214         df.setLenient(false);
 215 
 216         return df;
 217     }
 218 }