So the program is suppose to take a date in the format of "yyyy/dd/mm". After each date, there is text available to be entered. For example, "1776/7/4 First Independence Day". I wanted to use a String tokenizer and split the date at the slashes but then I am confused how to split it at the space after that. Once I split them, I can then compare the dates with other dates correct?
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first date: ");
String firstDate = sc.nextLine();
//System.out.print("Enter the second date: ");
//String secondDate = sc.nextLine();
//System.out.println();
//StringTokenizer date = new StringTokenizer(firstDate, "/",false);
//StringTokenizer space = new StringTokenizer(firstDate, " ",false);
//if (firstDate.equals(secondDate))
//{
// System.out.println (
// "There are 0 year(s), 0 month(s) and 0 day(s) between dates");
//}
int firstSpace = firstDate.indexOf(' ');
String event = firstDate.substring(firstSpace + 1); // "First Independence Day"
String date = firstDate.substring(0, firstSpace); // "1776/7/4"
// split on '/'
String dateParts[] = date.split("/");
String dateParts1[] = date.split("/");
String dateParts2[]= date.split("/");
if(dateParts1[1].length() != 2)
dateParts1[1] = "0" + dateParts1[1];
// add 0 to front of day
if(dateParts1[2].length() != 2)
dateParts1[2] = "0" + dateParts1[2];
// do the same for the second date
if(dateParts2[1].length() != 2)
dateParts2[1] = "0" + dateParts2[1];
// convert array to string
String date1 = String.join("/", dateParts1);
String date2 = String.join("/", dateParts2);
// set up the format that you have
SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy/MM/dd");
Date d1 = dateFormat.parse(date1);
Date d2 = dateFormat.parse(date2);
// equal dates
if(d1.equals(d2)) {
// There or 0 days, months and years between dates.
}