< prev index next >

src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java

Print this page




 102 import java.time.zone.ZoneRulesProvider;
 103 import java.util.AbstractMap.SimpleImmutableEntry;
 104 import java.util.ArrayList;
 105 import java.util.Arrays;
 106 import java.util.Collections;
 107 import java.util.Comparator;
 108 import java.util.HashMap;
 109 import java.util.HashSet;
 110 import java.util.Iterator;
 111 import java.util.LinkedHashMap;
 112 import java.util.List;
 113 import java.util.Locale;
 114 import java.util.Map;
 115 import java.util.Map.Entry;
 116 import java.util.Objects;
 117 import java.util.Set;
 118 import java.util.TimeZone;
 119 import java.util.concurrent.ConcurrentHashMap;
 120 import java.util.concurrent.ConcurrentMap;
 121 

 122 import sun.util.locale.provider.LocaleProviderAdapter;
 123 import sun.util.locale.provider.LocaleResources;
 124 import sun.util.locale.provider.TimeZoneNameUtility;
 125 
 126 /**
 127  * Builder to create date-time formatters.
 128  * <p>
 129  * This allows a {@code DateTimeFormatter} to be created.
 130  * All date-time formatters are created ultimately using this builder.
 131  * <p>
 132  * The basic elements of date-time can all be added:
 133  * <ul>
 134  * <li>Value - a numeric value</li>
 135  * <li>Fraction - a fractional value including the decimal place. Always use this when
 136  * outputting fractions to ensure that the fraction is parsed correctly</li>
 137  * <li>Text - the textual equivalent for the value</li>
 138  * <li>OffsetId/Offset - the {@linkplain ZoneOffset zone offset}</li>
 139  * <li>ZoneId - the {@linkplain ZoneId time-zone} id</li>
 140  * <li>ZoneText - the name of the time-zone</li>
 141  * <li>ChronologyId - the {@linkplain Chronology chronology} id</li>


 195 
 196     /**
 197      * Gets the formatting pattern for date and time styles for a locale and chronology.
 198      * The locale and chronology are used to lookup the locale specific format
 199      * for the requested dateStyle and/or timeStyle.
 200      *
 201      * @param dateStyle  the FormatStyle for the date, null for time-only pattern
 202      * @param timeStyle  the FormatStyle for the time, null for date-only pattern
 203      * @param chrono  the Chronology, non-null
 204      * @param locale  the locale, non-null
 205      * @return the locale and Chronology specific formatting pattern
 206      * @throws IllegalArgumentException if both dateStyle and timeStyle are null
 207      */
 208     public static String getLocalizedDateTimePattern(FormatStyle dateStyle, FormatStyle timeStyle,
 209             Chronology chrono, Locale locale) {
 210         Objects.requireNonNull(locale, "locale");
 211         Objects.requireNonNull(chrono, "chrono");
 212         if (dateStyle == null && timeStyle == null) {
 213             throw new IllegalArgumentException("Either dateStyle or timeStyle must be non-null");
 214         }
 215         LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased().getLocaleResources(locale);
 216         String pattern = lr.getJavaTimeDateTimePattern(
 217                 convertStyle(timeStyle), convertStyle(dateStyle), chrono.getCalendarType());

 218         return pattern;
 219     }
 220 
 221     /**
 222      * Converts the given FormatStyle to the java.text.DateFormat style.
 223      *
 224      * @param style  the FormatStyle style
 225      * @return the int style, or -1 if style is null, indicating un-required
 226      */
 227     private static int convertStyle(FormatStyle style) {
 228         if (style == null) {
 229             return -1;
 230         }
 231         return style.ordinal();  // indices happen to align
 232     }
 233 
 234     /**
 235      * Constructs a new instance of the builder.
 236      */
 237     public DateTimeFormatterBuilder() {




 102 import java.time.zone.ZoneRulesProvider;
 103 import java.util.AbstractMap.SimpleImmutableEntry;
 104 import java.util.ArrayList;
 105 import java.util.Arrays;
 106 import java.util.Collections;
 107 import java.util.Comparator;
 108 import java.util.HashMap;
 109 import java.util.HashSet;
 110 import java.util.Iterator;
 111 import java.util.LinkedHashMap;
 112 import java.util.List;
 113 import java.util.Locale;
 114 import java.util.Map;
 115 import java.util.Map.Entry;
 116 import java.util.Objects;
 117 import java.util.Set;
 118 import java.util.TimeZone;
 119 import java.util.concurrent.ConcurrentHashMap;
 120 import java.util.concurrent.ConcurrentMap;
 121 
 122 import sun.text.spi.JavaTimeDateTimePatternProvider;
 123 import sun.util.locale.provider.LocaleProviderAdapter;
 124 import sun.util.locale.provider.LocaleResources;
 125 import sun.util.locale.provider.TimeZoneNameUtility;
 126 
 127 /**
 128  * Builder to create date-time formatters.
 129  * <p>
 130  * This allows a {@code DateTimeFormatter} to be created.
 131  * All date-time formatters are created ultimately using this builder.
 132  * <p>
 133  * The basic elements of date-time can all be added:
 134  * <ul>
 135  * <li>Value - a numeric value</li>
 136  * <li>Fraction - a fractional value including the decimal place. Always use this when
 137  * outputting fractions to ensure that the fraction is parsed correctly</li>
 138  * <li>Text - the textual equivalent for the value</li>
 139  * <li>OffsetId/Offset - the {@linkplain ZoneOffset zone offset}</li>
 140  * <li>ZoneId - the {@linkplain ZoneId time-zone} id</li>
 141  * <li>ZoneText - the name of the time-zone</li>
 142  * <li>ChronologyId - the {@linkplain Chronology chronology} id</li>


 196 
 197     /**
 198      * Gets the formatting pattern for date and time styles for a locale and chronology.
 199      * The locale and chronology are used to lookup the locale specific format
 200      * for the requested dateStyle and/or timeStyle.
 201      *
 202      * @param dateStyle  the FormatStyle for the date, null for time-only pattern
 203      * @param timeStyle  the FormatStyle for the time, null for date-only pattern
 204      * @param chrono  the Chronology, non-null
 205      * @param locale  the locale, non-null
 206      * @return the locale and Chronology specific formatting pattern
 207      * @throws IllegalArgumentException if both dateStyle and timeStyle are null
 208      */
 209     public static String getLocalizedDateTimePattern(FormatStyle dateStyle, FormatStyle timeStyle,
 210             Chronology chrono, Locale locale) {
 211         Objects.requireNonNull(locale, "locale");
 212         Objects.requireNonNull(chrono, "chrono");
 213         if (dateStyle == null && timeStyle == null) {
 214             throw new IllegalArgumentException("Either dateStyle or timeStyle must be non-null");
 215         }
 216         LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(JavaTimeDateTimePatternProvider.class, locale);
 217         JavaTimeDateTimePatternProvider provider = adapter.getJavaTimeDateTimePatternProvider();
 218         String pattern = provider.getJavaTimeDateTimePattern(convertStyle(timeStyle),
 219                          convertStyle(dateStyle), chrono.getCalendarType(), locale);
 220         return pattern;
 221     }
 222 
 223     /**
 224      * Converts the given FormatStyle to the java.text.DateFormat style.
 225      *
 226      * @param style  the FormatStyle style
 227      * @return the int style, or -1 if style is null, indicating un-required
 228      */
 229     private static int convertStyle(FormatStyle style) {
 230         if (style == null) {
 231             return -1;
 232         }
 233         return style.ordinal();  // indices happen to align
 234     }
 235 
 236     /**
 237      * Constructs a new instance of the builder.
 238      */
 239     public DateTimeFormatterBuilder() {


< prev index next >