1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /* 4 ******************************************************************************* 5 * Copyright (C) 2008-2012, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 package com.ibm.icu.impl.javaspi.text; 10 11 import java.text.DateFormat; 12 import java.text.spi.DateFormatProvider; 13 import java.util.Locale; 14 15 import com.ibm.icu.impl.javaspi.ICULocaleServiceProvider; 16 import com.ibm.icu.impl.jdkadapter.SimpleDateFormatICU; 17 import com.ibm.icu.util.ULocale; 18 19 public class DateFormatProviderICU extends DateFormatProvider { 20 21 private static final int NONE = -1; 22 23 @Override getDateInstance(int style, Locale locale)24 public DateFormat getDateInstance(int style, Locale locale) { 25 return getInstance(style, NONE, locale); 26 } 27 28 @Override getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)29 public DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale locale) { 30 return getInstance(dateStyle, timeStyle, locale); 31 } 32 33 @Override getTimeInstance(int style, Locale locale)34 public DateFormat getTimeInstance(int style, Locale locale) { 35 return getInstance(NONE, style, locale); 36 } 37 38 @Override getAvailableLocales()39 public Locale[] getAvailableLocales() { 40 return ICULocaleServiceProvider.getAvailableLocales(); 41 } 42 getInstance(int dstyle, int tstyle, Locale locale)43 private DateFormat getInstance(int dstyle, int tstyle, Locale locale) { 44 com.ibm.icu.text.DateFormat icuDfmt; 45 ULocale actual = ICULocaleServiceProvider.toULocaleNoSpecialVariant(locale); 46 if (dstyle == NONE) { 47 icuDfmt = com.ibm.icu.text.DateFormat.getTimeInstance(tstyle, actual); 48 } else if (tstyle == NONE) { 49 icuDfmt = com.ibm.icu.text.DateFormat.getDateInstance(dstyle, actual); 50 } else { 51 icuDfmt = com.ibm.icu.text.DateFormat.getDateTimeInstance(dstyle, tstyle, actual); 52 } 53 if (!(icuDfmt instanceof com.ibm.icu.text.SimpleDateFormat)) { 54 // icuDfmt must be always SimpleDateFormat 55 return null; 56 } 57 58 return SimpleDateFormatICU.wrap((com.ibm.icu.text.SimpleDateFormat)icuDfmt); 59 } 60 } 61