1 /* 2 * Copyright (c) 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.time.LocalDate; 29 import java.time.chrono.Chronology; 30 import java.time.chrono.IsoChronology; 31 import java.time.format.DateTimeFormatter; 32 import java.time.format.FormatStyle; 33 import java.util.Locale; 34 35 import javafx.util.StringConverter; 36 import javafx.util.converter.LocalDateTimeStringConverter.LdtConverter; 37 38 /** 39 * <p>{@link StringConverter} implementation for {@link LocalDate} values.</p> 40 * 41 * @see LocalTimeStringConverter 42 * @see LocalDateTimeStringConverter 43 * @since JavaFX 8u40 44 */ 45 public class LocalDateStringConverter extends StringConverter<LocalDate> { 46 47 LdtConverter<LocalDate> ldtConverter; 48 49 // ------------------------------------------------------------ Constructors 50 51 /** 52 * Create a {@link StringConverter} for {@link LocalDate} values, using a 53 * default formatter and parser based on {@link IsoChronology}, 54 * {@link FormatStyle#SHORT}, and the user's {@link Locale}. 55 * 56 * <p>This converter ensures symmetry between the toString() and 57 * fromString() methods. Many of the default locale based patterns used by 58 * {@link DateTimeFormatter} will display only two digits for the year when 59 * formatting to a string. This would cause a value like 1955 to be 60 * displayed as 55, which in turn would be parsed back as 2055. This 61 * converter modifies two-digit year patterns to always use four digits. The 62 * input parsing is not affected, so two digit year values can still be 63 * parsed leniently as expected in these locales.</p> 64 */ 65 public LocalDateStringConverter() { 66 ldtConverter = new LdtConverter<LocalDate>(LocalDate.class, null, null, 67 null, null, null, null); 68 } 69 70 /** 71 * Create a {@link StringConverter} for {@link LocalDate} values, using a 72 * default formatter and parser based on {@link IsoChronology}, 73 * the specified {@link FormatStyle}, and the user's {@link Locale}. 74 * 75 * @param dateStyle The {@link FormatStyle} that will be used by the default 76 * formatter and parser. If null then {@link FormatStyle#SHORT} will be used. 77 */ 78 public LocalDateStringConverter(FormatStyle dateStyle) { 79 ldtConverter = new LdtConverter<LocalDate>(LocalDate.class, null, null, 80 dateStyle, null, null, null); 81 } 82 83 /** 84 * Create a {#link StringConverter} for {@link LocalDate} values using the supplied 85 * formatter and parser. 86 * 87 * <p>For example, to use a fixed pattern for converting both ways:</p> 88 * <blockquote><pre> 89 * String pattern = "yyyy-MM-dd"; 90 * DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); 91 * StringConverter<LocalDate> converter = 92 * DateTimeStringConverter.getLocalDateStringConverter(formatter, null); 93 * </pre></blockquote> 94 * 95 * Note that the formatter and parser can be created to handle non-default 96 * {@link Locale} and {@link Chronology} as needed. 97 * 98 * @param formatter An instance of {@link DateTimeFormatter} that will be 99 * used for formatting by the toString() method. If null then a default 100 * formatter will be used. 101 * @param parser An instance of {@link DateTimeFormatter} that will be used 102 * for parsing by the fromString() method. This can be identical to 103 * formatter. If null then formatter will be used, and if that is also null, 104 * then a default parser will be used. 105 */ 106 public LocalDateStringConverter(DateTimeFormatter formatter, DateTimeFormatter parser) { 107 ldtConverter = new LdtConverter<LocalDate>(LocalDate.class, formatter, parser, 108 null, null, null, null); 109 } 110 111 112 /** 113 * Create a StringConverter for {@link LocalDate} values using a default 114 * formatter and parser, which will be based on the supplied 115 * {@link FormatStyle}, {@link Locale}, and {@link Chronology}. 116 * 117 * @param dateStyle The {@link FormatStyle} that will be used by the default 118 * formatter and parser. If null then {@link FormatStyle#SHORT} will be used. 119 * @param locale The {@link Locale} that will be used by the default 120 * formatter and parser. If null then 121 * {@code Locale.getDefault(Locale.Category.FORMAT)} will be used. 122 * @param chronology The {@link Chronology} that will be used by the default 123 * formatter and parser. If null then {@link IsoChronology#INSTANCE} will be used. 124 */ 125 public LocalDateStringConverter(FormatStyle dateStyle, Locale locale, Chronology chronology) { 126 ldtConverter = new LdtConverter<LocalDate>(LocalDate.class, null, null, 127 dateStyle, null, locale, chronology); 128 } 129 130 // ------------------------------------------------------- Converter Methods 131 132 /** {@inheritDoc} */ 133 @Override public LocalDate fromString(String value) { 134 return ldtConverter.fromString(value); 135 } 136 137 /** {@inheritDoc} */ 138 @Override public String toString(LocalDate value) { 139 return ldtConverter.toString(value); 140 } 141 142 }