I'm a fresh person to Java. how can i split a date value(dd-mm-yyyy) intoyear,month,day parts. can any one help me for this with coding.
Well if the date is just a string then just usejava.lang.String.split(). ie.String dt = "01-23-2004";String dateParts[] = dt.split("-");String month = dateParts[0];String day = dateParts[1];String year = dateParts[2];If you're using an actual "Date" class like java.util.Date orjava.sql.Date then you can use the java.util.Calendar class to get thepieces of the date. ie.Date dt;...Calendar cal = Calendar.getInstance();cal.setTime(dt);int month = cal.get(Calendar.MONTH) + 1;int day = cal.get(Calendar.DATE);int year = cal.get(Calendar.YEAR);
refer java.util.Calendar API (Java docs)
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");ParsePosition parsePosition = new ParsePosition(0);Calendar cal = Calendar.getInstance();cal.setTime(dateFormatter.parse("03/03/2004",newParsePosition(0)));This way you will get a refernce to calendar object.Now you can get the values from the ref by usingint month = cal.get(Calendar.MONTH) + 1;int day = cal.get(Calendar.DATE);int year = cal.get(Calendar.YEAR);