Date formatting based on user locale on android

前端 未结 6 843
孤城傲影
孤城傲影 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 04:59

    While the accepted answer was correct when the question was asked, it has later become outdated. I am contributing the modern answer.

    java.time and ThreeTenABP

        DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
    
        LocalDate dateOfBirth = LocalDate.of(1991, Month.OCTOBER, 13);
        String formattedDob = dateOfBirth.format(dateFormatter);
        System.out.println("Born: " + formattedDob);
    

    It gives different output depending on the locale setting of the JVM (usually taking from the device). For example:

    • Canadian French: Born: 91-10-13
    • Chinese: Born: 1991/10/13
    • German: Born: 13.10.91
    • Italian: Born: 13/10/91

    If you want a longer format, you may specify a different format style. Example outputs in US English locale:

    • FormatStyle.SHORT: Born: 10/13/91
    • FormatStyle.MEDIUM: Born: Oct 13, 1991
    • FormatStyle.LONG: Born: October 13, 1991
    • FormatStyle.FULL: Born: Thursday, October 13, 1991

    Question: Can I use java.time on Android?

    Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

    • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
    • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
    • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

    Question: Can I also accept user input in the user’s local format?

    Yes, you can. The formatter can also be used for parsing a string from the user into a LocalDate:

        LocalDate date = LocalDate.parse(userInputString, dateFormatter);
    

    I suggest that you first format an example date and show it to the user so that s/he can see which format your program expects for his/her locale. As example date take a date with day of month greater than 12 and year greater than 31 so that the order of day, month and year can be seen from the example (for longer formats the year doesn’t matter since it will be four digits).

    Parsing will throw a DateTimeParseException if the user entered the date in an incorrect format or a non-valid date. Catch it and allow the user to try again.

    Question: Can I do likewise with a time of day? A date and time?

    Yes. For formatting a time of day according to a user’s locale, get a formatter from DateTimeFormatter.ofLocalizedTime. For both date and time together use one of the overloaded versions of DateTimeFormatter.ofLocalizedDateTime.

    Avoid the DateFormat, SImpleDateFormat and Date classes

    I recommend you don’t use DateFormat, SimpleDateFormat and Date. Those classes are poorly designed and long outdated, the first two in particular notoriously troublesome. Instead use LocalDate, DateTimeFormatter and other classes from java.time, the modern Java date and time API.

    Links

    • Oracle tutorial: Date Time explaining how to use java.time.
    • Java Specification Request (JSR) 310, where java.time was first described.
    • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
    • ThreeTenABP, Android edition of ThreeTen Backport
    • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.

提交回复
热议问题