2

I am trying to get the date format for Pakistan locale "en_PK" which is ""dd/MM/yy"

Locale localePK = new Locale("en", "PK");
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, localePK);

The above snippet returns "MM/dd/yy"

But the locale seems to be unsupported and not available in the DateFormat.getAvailableLocales() List.

Please suggest an ideal way to resolve this. Thanks in advance.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • You could write a wrapper class that returns a formatted date. Use the Locale when it's available and use a specific DateTimeFormatter when the Locale is absent. – Gilbert Le Blanc Oct 07 '20 at 16:40
  • Reopened it as the problem that OP has mentioned is how to find `Pakistan`'s locale from `DateFormat.getAvailableLocales()` List. – Arvind Kumar Avinash Oct 10 '20 at 08:23

1 Answers1

0

Search for the country name using the Locale object while navigating DateFormat.getAvailableLocales() and break the loop once you've found it.

import java.text.DateFormat;
import java.time.LocalDate;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        Locale localePk = null;
        for (Locale locale : DateFormat.getAvailableLocales()) {
            if (locale.getDisplayCountry().equals("Pakistan")) {
                localePk = locale;
                break;
            }
        }
        if (localePk != null) {
            String datePattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, null,
                    IsoChronology.INSTANCE, localePk);
            System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern(datePattern, localePk)));
        }
    }
}

Output:

10/10/2020

Note that DateTimeFormatterBuilder is part of the modern date-time API.

If you are doing it for your Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

java.util date-time classes are outdated and error-prone and so is their formatting API, DateFormat. However, if you are still looking for a solution using these legacy APIs, given below is one:

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        Locale localePk = null;
        for (Locale locale : DateFormat.getAvailableLocales()) {
            if (locale.getDisplayCountry().equals("Pakistan")) {
                localePk = locale;
                break;
            }
        }
        if (localePk != null) {
            DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, localePk);
            System.out.println(df.format(new Date()));
        }
    }
}

Output:

10/10/2020
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110