Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does java.text.DateFormat return the same date format for en_US and en_GB on Android?

I have a small Android application which I'm using to print a specific date in different formats based on locale.

Here is my code (using java.text.DateFormat):

Locale[] locales = {new Locale("en", "US"), new Locale("en", "GB"), new Locale("en", "AU"), new Locale("en", "NZ"), new Locale("en", "ZA")};
for(int i = 0; i < locales.length; ++i) {
    Log.d(logKey, locales[i].toString() + " - " + DateFormat.getDateInstance(DateFormat.SHORT, locales[i]).format(Calendar.getInstance().getTime()));
}

The output from this in LogCat is thus:

D/FormatPoC(  390): en_US - 4/27/12
D/FormatPoC(  390): en_GB - 4/27/12
D/FormatPoC(  390): en_AU - 4/27/12
D/FormatPoC(  390): en_NZ - 4/27/12
D/FormatPoC(  390): en_ZA - 4/27/12

So my question is - why are all of these the same? In Java SE I get:

en_US - 4/27/12
en_GB - 27/04/12
en_AU - 27/04/12
en_NZ - 27/04/12
en_ZA - 2012/04/27

Which is what I would expect. I know that I can use android.text.format.DateFormat to get correct results based on the user's current locale and date order setting, but that doesn't explain why using java.text.DateFormat to get the format for a programmatically specified locale doesn't return the right results.

Additionally, it's not just the SHORT date format - MEDIUM and LONG show inconsistencies between Android and Java SE as well (i.e. Android returns the same format for all 5 Locales I specified).

I've tested it on 3 different devices (2.3 and 4.0) and on the emulator (2.3 and 4.0), all with the same results. I've also tested using Locale.US and Locale.UK just to see if they're somehow different, but the results are the same.

Has anyone else run into this, or know why this would be?

UPDATE: 2012-07-18

It appears that this is an issue with the emulator, as well as many devices manufactured in the US. Using Dalvik Explorer:

https://play.google.com/store/apps/details?id=org.jessies.dalvikexplorer&hl=en

I've been able to see what the system returns for en_GB on different devices (including the emulator). Some return the appropriate formats, some return the en_US format. I assume this is simply an issue of what format resources are built into the OS for each device, though being that the emulator returns the wrong formats as well as many of my US-manufactured devices, I wonder what British developers think, or if they've seen this problem.

like image 636
mWillis Avatar asked Apr 27 '12 18:04

mWillis


People also ask

What is the use of dateformat in Java?

The java.text.DateFormat class provides various methods to format and parse date and time in java in language independent manner. The DateFormat class is an abstract class. java.text.Format is the parent class and java.text.SimpleDateFormat is the subclass of java.text.DateFormat class.

How do you format a date in Java?

Java Date Format. There are two classes for formatting date in java: DateFormat and SimpleDateFormat. The java.text.DateFormat class provides various methods to format and parse date and time in java in language independent manner. The DateFormat class is an abstract class.

Why is date format so hard to learn in Java?

If you find it difficult, it's because nothing about date format in Java is, or ever will be easy. Just so you know. @Johnathan: This is where I was getting completely confused.

What is the difference between formatting and parsing in Java?

In java, converting date into string is called formatting and vice-versa parsing. In other words, formatting means date to string and parsing means string to date.


1 Answers

This is not an answer (I do not yet have enough rep to add a comment...)

As a UK developer I have come across this problem. I am seeing this problem with my Galaxy S3, exactly as you have described.

I'm having to resort to allowing the user to choose the date format as a preference. Not very good.

The DalvikExplorer program also shows the problem:

enter image description here

like image 58
Forge_7 Avatar answered Oct 23 '22 12:10

Forge_7