1

My requirement is to format a time, based on a specific locale.

I have written a Java program to format current time based on some locales and, it gives the output as shown below:

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

public class DateFormatDemo {


  static public void showTimeStyles(Locale currentLocale) {

    Date today = new Date();
    String result;
    DateFormat formatter;

    int[] styles = { DateFormat.DEFAULT, DateFormat.SHORT, DateFormat.MEDIUM,
        DateFormat.LONG, DateFormat.FULL };

    System.out.println();
    System.out.println("Locale: " + currentLocale.toString());
    System.out.println();

    for (int k = 0; k < styles.length; k++) {
      formatter = DateFormat.getTimeInstance(styles[k], currentLocale);
      result = formatter.format(today);
      System.out.println(result);
    }
  }

  static public void main(String[] args) {

    showTimeStyles(new Locale("en", "US"));
    showTimeStyles(new Locale("fr", "FR"));
    showTimeStyles(new Locale("de", "DE"));

  }
}

The output of the above Java Program is:

Locale: en_US

9:49:37 AM
9:49 AM
9:49:37 AM
9:49:37 AM UTC
9:49:37 AM UTC

Locale: fr_FR

09:49:37
09:49
09:49:37
09:49:37 UTC
09 h 49 UTC

Locale: de_DE

09:49:37
09:49
09:49:37
09:49:37 UTC
09:49 Uhr UTC

So, basically for en-US the time format will be in 12-hour format and for fr-FR, the time format will be in the 24-hours format.

However, when i use the same code in Android application as shown below:

public void showTimeStyles(Locale currentLocale) {

        Date today = new Date();
        String result;
        DateFormat formatter;

        int[] styles = { DateFormat.DEFAULT, DateFormat.SHORT, DateFormat.MEDIUM,
                DateFormat.LONG, DateFormat.FULL };

        Log.d("MY_APP","Locale: " + currentLocale.toString());

        for (int k = 0; k < styles.length; k++) {
            formatter = DateFormat.getTimeInstance(styles[k], currentLocale);
            result = formatter.format(today);
            Log.d("MY_APP",result);
        }
    }

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        final View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        showTimeStyles(new Locale("en", "US"));
        showTimeStyles(new Locale("fr", "FR"));
        showTimeStyles(new Locale("de", "DE"));

        return rootView;
    }

The output of the above android program is shown below:

D/MY_APP: Locale: en_US
D/MY_APP: 3:28:19 PM
D/MY_APP: 3:28 PM
D/MY_APP: 3:28:19 PM
D/MY_APP: 3:28:19 PM GMT+05:30
D/MY_APP: 3:28:19 PM India Standard Time

D/MY_APP: Locale: fr_FR
D/MY_APP: 3:28:19 PM
D/MY_APP: 3:28 PM
D/MY_APP: 3:28:19 PM
D/MY_APP: 15:28:19 GMT+05:30
D/MY_APP: 15:28:19 heure de l’Inde

D/MY_APP: Locale: de_DE
D/MY_APP: 3:28:19 nachm.
D/MY_APP: 3:28 nachm.
D/MY_APP: 3:28:19 nachm.
D/MY_APP: 15:28:19 GMT+05:30
D/MY_APP: 15:28:19 Indische Zeit

Now, if we compare the output of both the Java code and android code, For the locale "fr-FR", the time is displayed in 12-hour format in Android and 24-hour format in Java.

The correct output is the output we get from Java code. That is in French, the standard time format is 24-hour format and not 12-hour format with AM/PM.

Why is the code behavior different?

How do we get localized time format in android for a specific locale?

Is it possible to get localized time format in Joda-Time for Android, if so how?

Using Joda-time i have tried the below code, but the output was still in 12-hour format for "fr-FR":

//Convert the received timestamp to Joda DateTime
DateTime dt = new DateTime(date);
// translate the received DateTime to the time in the User's Time Zone
DateTime dateInUserTimeZone = dt.withZone(DateTimeZone.forID(User.getInstance().getTimeZone()));
String pattern = DateTimeFormat.patternForStyle("MM", new Locale("fr","FR"));
DateTimeFormatter formatterOutput1 = DateTimeFormat.forPattern(pattern);
Log.d("MY_APP","DateTime:"+formatterOutput1.print(dateInUserTimeZone));
ThomasW
  • 16,981
  • 4
  • 79
  • 106
Zax
  • 2,870
  • 7
  • 52
  • 76
  • `D/MY_APP: 3:28:19 PM` have you used "HH:mm"? – Ashwin Mothilal Mar 21 '17 at 10:24
  • @AshwinMothilal: The format must be decided based on the Locale. Like fr-FR locale used 24-hour format and en-US uses 12-hour format. I must not be explicitly specifying any format. Everything must be decided based on the locale. – Zax Mar 21 '17 at 10:30
  • Sorry didn't check you code. If it is based upon Locale then can you use `Locale.getDefault()` The JVM sets the default locale during startup based on the host environment. – Ashwin Mothilal Mar 21 '17 at 10:53
  • Time format depends on phone settings. See Settings - System - Date & Time - Use 24-hour format – 1615903 Mar 21 '17 at 11:05
  • I don't get you sorry bro. If you say in depends on phone settings. Then try changing it to 24hr and test it. Otherwise, let me join you and wait for help. – Ashwin Mothilal Mar 21 '17 at 11:34
  • I have heard it said that you should prefer the [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) over JodaTime (I suspect it’s ABP for Android backport). – Ole V.V. Mar 21 '17 at 11:42
  • @AshwinMothilal: What i mean to say is, the time format must be decided based on the locale which i will be dynamically setting using the new Locale("fr","FR") constructor. – Zax Mar 21 '17 at 11:47
  • I have just tried as your code, but it works fine as you said. The date format `Locale: fr_FR` just like `21:33:21`. Which android version do you use? – Jason Mar 21 '17 at 13:34
  • @Paul: I'm using Android 6.0, Moto G2. – Zax Mar 21 '17 at 14:06
  • You can use reflect mechanisms to see the attribute "pattern" in your `formatter`. The value "HH:mm:ss" should be 24-hours format. – Jason Mar 21 '17 at 14:55
  • You might want to see this [guideline](https://developer.android.com/reference/java/text/SimpleDateFormat.html). You can just use pattern to control the format. – Jason Mar 21 '17 at 15:01
  • Yes, @Paul, but that would require the asker to do that for every possible locale. – Ole V.V. Mar 22 '17 at 09:37
  • 3
    Is it possible that different formatters are being used? In the java code, you show that you are using java.text.DateFormat. Is it possible that in the android code you are using android.text.format.DateFormat? This class takes account of the user preference stored in the Settings app. – Breandán Dalton Mar 22 '17 at 13:11
  • 1
    It is also a mistake to assume that all devices have the same locales available. A device sold in the US will almost certainly support en_US and es_US, but not necessarily any locales with the same language but different countries (such as en_GB or es_ES), nor any locales for other languages (such as de_DE). The opposite may well be true for a device sold in Europe. – Jason Mar 22 '17 at 14:22

0 Answers0