How to get day,month,year from String date?

前端 未结 5 1244
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-02 06:27

I have a date on String, like this:

String date=\"25.07.2007\";

And I want to divide it to:

String day;
String month;
String yea         


        
5条回答
  •  忘掉有多难
    2021-01-02 06:46

    java.time

    The modern way is to use the Android version of the back-port of the java.time framework built into Java 8 and later.

    First, parse the input string into a date-time object. The LocalDate class represents a date-only value without time-of-day and without time zone.

    String input = "25.07.2007";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "MM.dd.yyyy" );
    LocalDate localDate = LocalDate.parse( input , formatter );
    

    Now you can ask the LocalDate for its values.

    int year = localDate.getYear();
    int month = localDate.getMonthValue();
    int dayOfMonth = localDate.getDayOfMonth();
    

    You can use the handy Month enum to localize the name of the month.

    String monthName = localDate.getMonth().getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH );
    

提交回复
热议问题