Date formatting based on user locale on android
You can use the DateFormat class that formats a date according to the user locale.
Example:
String dateOfBirth = "26/02/1974";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date = null;
try {
date = sdf.parse(dateOfBirth);
} catch (ParseException e) {
// handle exception here !
}
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(context);
String s = dateFormat.format(date);
You can use the different methods getLongDateFormat
, getMediumDateFormat
depending on the level of verbosity you would like to have.
Custom Android date formatting respecting current Locale
Android has DateFormat.getBestDateTimePattern()
to achieve this
pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), "MMMyyyy");
dayMonth = new SimpleDateFormat(pattern);
The only downside is the API level of 18. I personally used the result of DateFormat.getMediumDateFormat()
as fallback. Alternatively you could just ignore the locale for older devices and use new SimpleDateFormat("MMM yyyy")
there as fallback or try to backport this one method.
OP Edit
I tried on Nexus 5x with Android 6 a following code
for (Locale locale : locales) {
format = android.text.format.DateFormat.getBestDateTimePattern(locale, "MMMyyyy");
DateFormat dateFormat = new SimpleDateFormat(format, locale);
log.debug("{}: {}", locale.getCountry(), dateFormat.format(new Date()));
}
And here are some result:
- NA: Jul 2016
- AE: يوليو ٢٠١٦
- BG: 07.2016 г.
- CZ: červenec 2016
- FR: Goue 2016
- DE: Juli 2016
- GB: Jul 2016
- FI: heinä 2016
- IT: Lui 2016
- HR: srp 2016.
- RU: июль 2016
How can I format Date with Locale in Android
I am not sure if below code works for you, its written in Scala its almost same in Java.
val malaysia = new Locale("ms","MY","MY")
val cal = Calendar.getInstance(malaysia)
val df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, malaysia)
println(df.format(cal.getTime()))
results from above code is
Isnin 23 Mei 2016 9:55:44 AM IST
Show date based on the locale settings
I use DateFormat:
java.text.DateFormat dateFormat = DateFormat.getTimeFormat(context);
String dateString = dateFormat.format(date);
Keep in mind it also returns class named java.text.DateFormat but it's a different class.
Localizing dates in android
String dateOfBirth = "02/26/1974";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date date = null;
try {
date = sdf.parse(dateOfBirth);
} catch (Exception e) {
// handle exception here !
}
// get localized date formats
Log.i(this,"sdf default: "+new SimpleDateFormat().format(date)); // using my phone locale
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.US);
Log.i(this,"dateFormat US DEFAULT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.GERMAN);
Log.i(this,"dateFormat GERMAN DEFAULT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.CHINESE);
Log.i(this,"dateFormat CHINESE DEFAULT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
Log.i(this,"dateFormat US SHORT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
Log.i(this,"dateFormat GERMAN SHORT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINESE);
Log.i(this,"dateFormat CHINESE SHORT: "+dateFormat.format(date));
output is:
sdf default: 26.02.74 0:00
dateFormat US DEFAULT: Feb 26, 1974
dateFormat GERMAN DEFAULT: 26.02.1974
dateFormat CHINESE DEFAULT: 1974-2-26
dateFormat US SHORT: 2/26/74
dateFormat GERMAN SHORT: 26.02.74
dateFormat CHINESE SHORT: 74-2-26
How to show date format in both english and arabic?
Setting the Locale to "ar" will convert the date format from English to Arabic.
I have also used DateTimeFormatter
, as SimpleDateFormat
is outdated
fun parseDate() {
var formatter: DateTimeFormatter? = null
val date = "2021-11-03T14:09:31"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
val dateTime: LocalDateTime = LocalDateTime.parse(date, formatter)
val formatter2: DateTimeFormatter =
DateTimeFormatter.ofPattern(
"yyyy, MMM d", Locale("ar") // set the language in which you want to convert
// For english use Locale.ENGLISH
)
Log.e("Date", "" + dateTime.format(formatter2))
}
}
Note: DateTimeFormatter
only works in android 8 and above, to use it below android 8 enable desugaring
Output : 2021, نوفمبر 3
Edit: : If you want to convert numbers also to arabic use withDecimalStyle
with the DateTimeFormatter
.
val formatter2: DateTimeFormatter =
DateTimeFormatter.ofPattern(
"d MMM, yyyy",
Locale("ar")
).withDecimalStyle(
DecimalStyle.of(Locale("ar"))
)
Output: ٣ نوفمبر, ٢٠٢١
How to display date format according to locale?
String hintText = new SimpleDateFormat().toPattern(); // dd/MM/y HH:mm
I know you were asking for just the date, but this was as close as I can get.
The internal rule is localeData.getDateFormat(SHORT) + " " + localeData.getTimeFormat(SHORT)
so I guess as long as there is not a locale that has a date format with spaces on it, you can just split the string on a space
Date and time formatting depending on locale
Use android.text.format.DateFormat.getTimeFormat()
ref: http://developer.android.com/reference/android/text/format/DateFormat.html
Related Topics
How to Crop an Image in Android
Ndk Resolution Outcome: Project Settings: Gradle Model Version=5.4.1, Ndk Version Is Unknown Error
How to Get My Android Device Country Code Without Using Gps
Get List of Photo Galleries on Android
Android Java.Lang.Noclassdeffounderror
How to Create a Custom Notification Layout in Android
How to Get Access Token After User Is Signed in from Gmail in Android
Android:Static Fields and Memory Leaks
Preventing Status Bar Expansion
What Will Happen to the Sharedpreferences on Update an Android App
Android: How to Get a Radiogroup with Togglebuttons
How to Resolve Target 'Android-Xx'
Android: How to Select Texts from Webview
Custom Info Window Adapter with Custom Data in Map V2
Emulator in Android Studio Doesn't Start