Hi im trying to compare a user inputted date (as a string) with the current date so as to find out if the date is earlier or older.
My current code is
String date;
Date newDate;
Date todayDate, myDate;
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy");
while(true)
{
Scanner s = new Scanner (System.in);
date = s.nextLine();
Calendar cal = Calendar.getInstance();
try {
// trying to parse current date here
// newDate = dateFormatter.parse(cal.getTime().toString()); //throws exception
// trying to parse inputted date here
myDate = dateFormatter.parse(date); //no exception
} catch (ParseException e) {
e.printStackTrace(System.out);
}
}
Im trying to get both user input date and current date into two Date objects, so that i can use Date.compareTo() to simplify comparing dates.
I was able to parse the user input string into the Date object. However the current date cal.getTime().toString() does not parse into the Date object due to being an invalid string.
How to go about doing this? Thanks in advance