Date formatting based on user locale on android

前端 未结 6 841
孤城傲影
孤城傲影 2020-11-27 04:35

I want to display a date of birth based on the user locale. In my application, one of my fields is the date of birth, which is currently in the format dd/mm/yyyy

6条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 05:02

    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.

提交回复
热议问题