1 /*
2 * Copyright (c) 1999, 2012, 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 sun.util.locale.provider;
27
28 import java.text.DateFormat;
29 import java.text.SimpleDateFormat;
30 import java.text.spi.DateFormatProvider;
31 import java.util.Calendar;
32 import java.util.Locale;
33 import java.util.MissingResourceException;
34 import java.util.Set;
35
36 /**
37 * Concrete implementation of the {@link java.text.spi.DateFormatProvider
38 * DateFormatProvider} class for the JRE LocaleProviderAdapter.
39 *
40 * @author Naoto Sato
41 * @author Masayoshi Okutsu
42 */
43 public class DateFormatProviderImpl extends DateFormatProvider implements AvailableLanguageTags {
44 private final LocaleProviderAdapter.Type type;
45 private final Set<String> langtags;
46
47 public DateFormatProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
48 this.type = type;
49 this.langtags = langtags;
50 }
51
52 /**
53 * Returns an array of all locales for which this locale service provider
54 * can provide localized objects or names.
130 * <code>timeStyle</code> is invalid,
131 * or if <code>locale</code> isn't
132 * one of the locales returned from
133 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
134 * getAvailableLocales()}.
135 * @exception NullPointerException if <code>locale</code> is null
136 * @return a date/time formatter.
137 * @see java.text.DateFormat#getDateTimeInstance(int, int, java.util.Locale)
138 */
139 @Override
140 public DateFormat getDateTimeInstance(int dateStyle, int timeStyle,
141 Locale locale) {
142 return getInstance(dateStyle, timeStyle, locale);
143 }
144
145 private DateFormat getInstance(int dateStyle, int timeStyle, Locale locale) {
146 if (locale == null) {
147 throw new NullPointerException();
148 }
149
150 SimpleDateFormat sdf = new SimpleDateFormat("", locale);
151 Calendar cal = sdf.getCalendar();
152 try {
153 String pattern = LocaleProviderAdapter.forType(type)
154 .getLocaleResources(locale).getDateTimePattern(timeStyle, dateStyle,
155 cal);
156 sdf.applyPattern(pattern);
157 } catch (MissingResourceException mre) {
158 // Specify the fallback pattern
159 sdf.applyPattern("M/d/yy h:mm a");
160 }
161
162 return sdf;
163 }
164
165 @Override
166 public Set<String> getAvailableLanguageTags() {
167 return langtags;
168 }
169 }
|
1 /*
2 * Copyright (c) 1999, 2017, 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 sun.util.locale.provider;
27
28 import java.text.DateFormat;
29 import java.text.SimpleDateFormat;
30 import java.text.spi.DateFormatProvider;
31 import java.util.Calendar;
32 import java.util.Locale;
33 import java.util.MissingResourceException;
34 import java.util.Set;
35 import java.util.TimeZone;
36
37 /**
38 * Concrete implementation of the {@link java.text.spi.DateFormatProvider
39 * DateFormatProvider} class for the JRE LocaleProviderAdapter.
40 *
41 * @author Naoto Sato
42 * @author Masayoshi Okutsu
43 */
44 public class DateFormatProviderImpl extends DateFormatProvider implements AvailableLanguageTags {
45 private final LocaleProviderAdapter.Type type;
46 private final Set<String> langtags;
47
48 public DateFormatProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
49 this.type = type;
50 this.langtags = langtags;
51 }
52
53 /**
54 * Returns an array of all locales for which this locale service provider
55 * can provide localized objects or names.
131 * <code>timeStyle</code> is invalid,
132 * or if <code>locale</code> isn't
133 * one of the locales returned from
134 * {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
135 * getAvailableLocales()}.
136 * @exception NullPointerException if <code>locale</code> is null
137 * @return a date/time formatter.
138 * @see java.text.DateFormat#getDateTimeInstance(int, int, java.util.Locale)
139 */
140 @Override
141 public DateFormat getDateTimeInstance(int dateStyle, int timeStyle,
142 Locale locale) {
143 return getInstance(dateStyle, timeStyle, locale);
144 }
145
146 private DateFormat getInstance(int dateStyle, int timeStyle, Locale locale) {
147 if (locale == null) {
148 throw new NullPointerException();
149 }
150
151 // Check for region override
152 Locale rg = CalendarDataUtility.findRegionOverride(locale).orElse(locale);
153
154 SimpleDateFormat sdf = new SimpleDateFormat("", rg);
155 Calendar cal = sdf.getCalendar();
156 try {
157 String pattern = LocaleProviderAdapter.forType(type)
158 .getLocaleResources(rg).getDateTimePattern(timeStyle, dateStyle,
159 cal);
160 sdf.applyPattern(pattern);
161 } catch (MissingResourceException mre) {
162 // Specify the fallback pattern
163 sdf.applyPattern("M/d/yy h:mm a");
164 }
165
166 // Check for timezone override
167 String tz = locale.getUnicodeLocaleType("tz");
168 if (tz != null) {
169 sdf.setTimeZone(
170 TimeZoneNameUtility.convertLDMLShortID(tz)
171 .map(TimeZone::getTimeZone)
172 .orElseGet(sdf::getTimeZone));
173 }
174
175 return sdf;
176 }
177
178 @Override
179 public Set<String> getAvailableLanguageTags() {
180 return langtags;
181 }
182 }
|