Frage

I have a problem, I need the universal functions for showing date and time in any locale separately. But I can't find the way to do it without the checking the calendar.getLocale()

this function will give the date in US locale

static public String getDateFromCalendar(Calendar cal) {

        return String.format("%tD", cal);
    }

But if the Locale is russian I have to use istead this: String.format("%td.%tm.%tY", cal); I don't want to use the conditional operations for every possible locale. Please help to find the way to do is simplier.

War es hilfreich?

Lösung

Assuming you mean Java, I suggest you to consider the class java.text.DateFormat. Background is that every country/locale has its own typical date-time-format. For example:

public static String getDateFromCalendar(Calendar cal) {
    // maybe get user-locale via ThreadLocal or via second method parameter
    Locale locale = new Locale("ru", "Ru"); 

    DateFormat dateFormat = 
      DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
    return dateFormat.format(cal.getTime());
}

You can adjust the format style by choosing between SHORT, MEDIUM, LONG or FULL. For MEDIUM the output is: 05.04.2014 Compare that with the output for Locale.US yielding: Apr 5, 2014.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top