-3

I have this string 10-12-1999 and I want to store the values into a different variables like this:

So I have this string data= 10-12-1999 and I want to store it like this

int day =10;
int month = 12;
int year = 1999;

Can anyone point me into the right direction?

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73

2 Answers2

6
String data = "10-12-1999"
String[] dataArray = data.split("-");
int day = Integer.parseInt(dataArray[0])
int month = Integer.parseInt(dataArray[1])
int year = Integer.parseInt(dataArray[2])
wastl
  • 2,643
  • 14
  • 27
0

You need to use the public String[] split(String regex) method available in the String class :

I hope this helps :-

String date="10-12-1999";
String [] s1=date.split("-");
int day=Integer.parseInt(s1[0]);
int month=Integer.parseInt(s1[1]);
int year=Integer.parseInt(s1[2]);
System.out.println("Day->"+day+"    Month->"+month+"    Year->"+year); 

This will help extracting out all the date elements and you can assign them to all the requisite variables!

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73