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));