src/share/classes/java/time/format/DateTimeFormatStyleProvider.java

Print this page

        

*** 59,74 **** * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package java.time.format; - import java.text.DateFormat; import java.text.SimpleDateFormat; ! import java.time.temporal.Chrono; import java.util.Locale; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; /** * A provider to obtain date-time formatters for a style. * <p> * --- 59,75 ---- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package java.time.format; import java.text.SimpleDateFormat; ! import java.time.chrono.Chronology; import java.util.Locale; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; + import sun.util.locale.provider.LocaleProviderAdapter; + import sun.util.locale.provider.LocaleResources; /** * A provider to obtain date-time formatters for a style. * <p> *
*** 108,154 **** * @param locale the locale to use, not null * @return the date-time formatter, not null * @throws IllegalArgumentException if both format styles are null or if the locale is not recognized */ public DateTimeFormatter getFormatter( ! FormatStyle dateStyle, FormatStyle timeStyle, Chrono<?> chrono, Locale locale) { if (dateStyle == null && timeStyle == null) { throw new IllegalArgumentException("Date and Time style must not both be null"); } String key = chrono.getId() + '|' + locale.toString() + '|' + dateStyle + timeStyle; Object cached = FORMATTER_CACHE.get(key); if (cached != null) { - if (cached.equals("")) { - throw new IllegalArgumentException("Unable to convert DateFormat to DateTimeFormatter"); - } return (DateTimeFormatter) cached; } ! DateFormat dateFormat; ! if (dateStyle != null) { ! if (timeStyle != null) { ! dateFormat = DateFormat.getDateTimeInstance(convertStyle(dateStyle), convertStyle(timeStyle), locale); ! } else { ! dateFormat = DateFormat.getDateInstance(convertStyle(dateStyle), locale); ! } ! } else { ! dateFormat = DateFormat.getTimeInstance(convertStyle(timeStyle), locale); ! } ! if (dateFormat instanceof SimpleDateFormat) { ! String pattern = ((SimpleDateFormat) dateFormat).toPattern(); DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern(pattern).toFormatter(locale); FORMATTER_CACHE.putIfAbsent(key, formatter); return formatter; } - FORMATTER_CACHE.putIfAbsent(key, ""); - throw new IllegalArgumentException("Unable to convert DateFormat to DateTimeFormatter"); - } /** ! * Converts the enum style to the old format style. ! * @param style the enum style, not null ! * @return the int style */ private int convertStyle(FormatStyle style) { return style.ordinal(); // indices happen to align } } --- 109,147 ---- * @param locale the locale to use, not null * @return the date-time formatter, not null * @throws IllegalArgumentException if both format styles are null or if the locale is not recognized */ public DateTimeFormatter getFormatter( ! FormatStyle dateStyle, FormatStyle timeStyle, Chronology chrono, Locale locale) { if (dateStyle == null && timeStyle == null) { throw new IllegalArgumentException("Date and Time style must not both be null"); } String key = chrono.getId() + '|' + locale.toString() + '|' + dateStyle + timeStyle; Object cached = FORMATTER_CACHE.get(key); if (cached != null) { return (DateTimeFormatter) cached; } ! ! LocaleResources lr = LocaleProviderAdapter.getResourceBundleBased() ! .getLocaleResources(locale); ! String pattern = lr.getCldrDateTimePattern(convertStyle(timeStyle), convertStyle(dateStyle), ! chrono.getCalendarType()); DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern(pattern).toFormatter(locale); FORMATTER_CACHE.putIfAbsent(key, formatter); return formatter; } /** ! * Converts the enum style to the java.util.Calendar style. Standalone styles ! * are not supported. ! * ! * @param style the enum style ! * @return the int style, or -1 if style is null, indicating unrequired */ private int convertStyle(FormatStyle style) { + if (style == null) { + return -1; + } return style.ordinal(); // indices happen to align } }