Getting wrong month when using SimpleDateFormat.parse

前端 未结 3 1512
长情又很酷
长情又很酷 2020-11-27 23:59

In my programm there is a very strange problem. Here you can see String birthday and Log to check it:

birthday = String.valueOf(bir         


        
3条回答
  •  庸人自扰
    2020-11-28 00:10

    ThreeTenABP

    I should like to provide the 2017 answer. For Android, first get the ThreeTenABP library. Instructions are in this question: How to use ThreeTenABP in Android Project. Then the rest is as straightforward as:

        LocalDate birthDate = LocalDate.of(birthYear, birthMonth, birthDay);
        System.out.println("Birth date  : " + birthDate);
    

    This prints:

    Birth date  : 1999-10-15
    

    When you’ve got the year, month and day of month as numbers, there is no need to do any parsing, that would just complicate things.

    ThreeTenABP is the backport of java.time or JSR-310, the modern Java date and time API, to Android Java 7. If you are using Java 8 or later, java.time is built-in. There is also a backport for (non-Android) Java 6 and 7: ThreeTen Backport.

提交回复
热议问题