-1

I have the String = "08/21/2018".

How can I get only "08" from this line and insert this value into a int?

FInal result must be int month = 01.

2 Answers2

1

You need to split string and need to get 0 index value and parse that in an integer value.As shown in below code.

    String date = "08/21/2018";
    String [] splitDate = date.split("/");
    int month = Integer.valueOf(splitDate[0]);
Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
1
  String date= "08/21/2018";
            String[] arr=date.split(Pattern.quote("/"));
            int month = Integer.parseInt(arr[0]);
            int day = Integer.parseInt(arr[1]);
            int year = Integer.parseInt(arr[2]);
kuber singh
  • 289
  • 1
  • 5