ereOn ereOn - 1 month ago 10
Android Question

Date and time formatting depending on locale

I'm new to both Java and Android development so this might be a stupid question, but I've been searching for days now and can't find a solution:

I try to output a

java.util.Date
depending on the user's locale.

Searching on StackOverflow lead me to this:

java.util.Date date = new Date();
String dateString = DateFormat.getDateFormat(getApplicationContext()).format(date);


This outputs:

20/02/2011


On my french localized phone. Almost fine.

How can I also output the hours, minutes and seconds parts of the
Date
, using the user's locale ? I've been looking in the android documentation but couldn't find anything.

Many thanks.

Answer

Use android.text.format.DateFormat.getTimeFormat()

ref: http://developer.android.com/reference/android/text/format/DateFormat.html