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

前端 未结 5 1095
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  -上瘾入骨i
    2021-01-02 06:47

    Try this...

    String dateArr[] = date.split(".");
    String day = dateArr[0];
    String month = dateArr[1];
    String year = dateArr[2];
    

    or

    String day = date.substring(0,2);
    String month = date.substring(3,5);
    String year = date.substring(6,10);
    

    To get the current date...

    Date currentDate = Calendar.getInstance().getTime();
    

    To get day, month and year from current date..

    int day = currentDate.get(Calendar.DAY_OF_MONTH);
    int month = currentDate.get(Calendar.MONTH);
    int year = currentDate.get(Calendar.YEAR);
    

    Month starts at 0 here...

提交回复
热议问题