Getting Localized Date for Today in Android

Getting Localized Date for Today in Android

I've been doing it all wrong.

As most seasoned developers know, making an application that works for many countries and languages is pretty much a requirement of modern software development.

And I've been doing it the hard way...

So, here's a snippet to get today's date in Android into a string that is formatted for the local language. Now, those not in the know may be thinking "A date's a date, right?" Wrong. American's have most things backwards. Dates are one of them. We're just so insulated that we don't realize it. For instance, today's date in the great USA looks like "10/3/2013" but in most European countries it would look like "3-10-2013" - progressing from smallest part of time to the largest.

So here's that snippet I promised (if you're in the UK, is that "promized?"):

Calendar c = Calendar.getInstance();
String mydate= android.text.format.DateFormat.getDateFormat(context).format(c.getTime());

DateFormat in Android will automatically structure the string in the way it's needed for whatever localization settings are on the device. Now you know. Yes, I'm slow to this, but that's ok.

Posted by Tony on Oct 03, 2013 | Android Development