1

I am using the Joda library. My requirement is that the displayed date string is appropriate for the user's locale, i.e on a german device 12.12.2014 and on a american device 2014/12/12. I found that I can use the toString() Method of LocalDate.

LocalDate localDate = LocalDate.now();
localDate.toString("yyyy/MM/dd", Locale.getDefault());

If I understand right, I need to supply a pattern, but IMHO this defeats the purpose of specifying a locale.

Can anybody enlighten me?

mrd
  • 4,561
  • 10
  • 54
  • 92
  • **For future visitors of this page**: Now, the [Home Page of Joda-Time](https://www.joda.org/joda-time/) has this notice: *Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project*. You can check [this answer](https://stackoverflow.com/a/67449876/10819573) using the `java.time` API. – Arvind Kumar Avinash May 09 '21 at 10:47

2 Answers2

3

My solution, which will format the displayed date appropriate for the device's locale:

        DateTimeFormatter fmt = DateTimeFormat.mediumDate();
        String str = fmt.print(localDate);
        getGeburtsdatumEditText().setText(str);
mrd
  • 4,561
  • 10
  • 54
  • 92
-1

I use this code for similar case for generate hours and minutes:

public String timeGenerateString( int iHour, int iMinute) {
  if( (iHour   < 0) || (iHour   > 24))  return "";
  if( (iMinute < 0) || (iMinute > 59))  return "";

  if( android.text.format.DateFormat.is24HourFormat( getApplicationContext()) == true)
    return  "" + String.format("%02d", iHour) + ":" + String.format("%02d", iMinute);
   else
    if( iHour >= 13)
      return  "" + String.format("%02d", (iHour - 12)) + ":" + String.format("%02d", iMinute) + " PM";
     else
      return  "" + String.format("%02d", iHour) + ":" + String.format("%02d", iMinute) + " AM";
}

May be this will be usually for you.

Tapa Save
  • 4,769
  • 5
  • 32
  • 54