Search code examples
androidxamarinxamarin.androiddevelopment-environment

Time Format("h a") not localized to french in Xamarin.Android


I have try to localize the time format(AM/PM) in Xamarin.Android by using SimpleDateFormat like below code example :

Code example:

Calendar time = Calendar.GetInstance(Locale.Default);

string timeText = new SimpleDateFormat("h a", new Locale("fr")).Format(time.Time).ToLower();

But it will always localize only for English(9 am). Please suggest am I missed anythig to localize string. Thanks in advance.


Solution

  • You did wrong to create a new Locale for fr, to use given pattern and the default data format symbols for the given locale "France", you can for example code like this:

    Android.Icu.Util.Calendar time = Android.Icu.Util.Calendar.GetInstance(Locale.Default);
    var format = new SimpleDateFormat("h:mm a", Android.Icu.Util.ULocale.French);
    var timetext = format.Format(time.Time);
    

    For more information, you can refer to [SimpleDateFormat (String pattern, Locale locale)](https://developer.android.com/reference/java/text/SimpleDateFormat.html#SimpleDateFormat(java.lang.String, java.util.Locale)).