Java by API/java.text.spi/DateFormatProvider

Материал из Java эксперт
Перейти к: навигация, поиск

extends DateFormatProvider

  

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.spi.DateFormatProvider;
import java.util.Date;
import java.util.Locale;
public class Main {
    public static void main(String [] args) {
        Date now = new Date();
        DateFormat defaultFormat = DateFormat.getDateTimeInstance();
        String defaultString = defaultFormat.format (now);
        System.out.println ("Default : " + defaultString);
        DateFormat antarcticaFormat =
                DateFormat.getDateTimeInstance (
                DateFormat.FULL, DateFormat.FULL,
                new Locale ("en", "AQ"));
        String antarcticaString = antarcticaFormat.format (now);
        System.out.println ("Antarctica: " + antarcticaString);
    }
}
class DateFormatProviderImpl extends DateFormatProvider {
    private Locale ca = new Locale ("en", "CA");
    public Locale[] getAvailableLocales() {
        return new Locale [] {ca};
    }
    public DateFormat getTimeInstance(int style, Locale locale) {
        if (locale.equals(ca)) {
            return new SimpleDateFormat("HH.mm.ss");
        }
        return null;
    }
    public DateFormat getDateTimeInstance(int dateStyle, Locale locale) {
        if (locale.equals(ca)) {
            return new SimpleDateFormat("yyyy");
        }
        return null;
    }
    public DateFormat getDateTimeInstance(int dateStyle,
           int timeStyle, Locale locale) {
        if (locale.equals(ca)) {
            return new SimpleDateFormat("yyyy/MM/dd");
        }
        return null;
    }
    @Override
    public DateFormat getDateInstance(int style, Locale locale) {
      if (locale.equals(ca)) {
        return new SimpleDateFormat("MM/dd");
      }
      return null;
    }
}