Nawin Khatiwada Nawin Khatiwada - 1 year ago 51
Android Question

Convert String Date to integer and Get Month

I want to convert String Date to integer and get the month from that integer how can I do that??

For Example:
I have String Date as:

String date = "15-06-2016";

so how can I get month as:

06 as output in integer

Answer Source

You don't need to parse that to date to then get the number of the month, that conversion is not necessary (you could but is a waste of memory and computational time).....

use regex, split the string and parsing the 2nd element of the array will get that directly...

public static void main(String[] args) {
    String date = "15-06-2016";
    String[] calend = date.split("-");
    int month = Integer.parseInt(calend[1]);
    System.out.println("the month is " + month);