Reputation: 215
I have written the code to spit the string i.e birthdate,I want to store it in 3 different vaiables how to do it. (Mday=1,Mmonth=1,MYear=2011).I am getting birtdate dynamically.also I am getting values in token.
StringTokenizer st = new StringTokenizer(BirtDate, "/");
while (st.hasMoreElements()) {
String token = st.nextToken();
System.out.println("Token = " + token);
}
Upvotes: 0
Views: 2413
Reputation: 34387
One better way could be to use date formatter and object as below:
DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date date = format.parse("1/1/2012");
//Calendar cal = new GregorianCalendar();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_MONTH);//You may want to add 1
int month = cal.get(Calendar.MONTH);
int year= cal.get(Calendar.YEAR);
Please note: Month starts from 0, so you may want to add 1.
Upvotes: 0
Reputation: 1055
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead
See http://docs.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html
Use instead
String[] token = BirtDate.split("/")
Upvotes: 2