您好我试图将用户输入的日期(作为字符串)与当前日期进行比较,以便确定日期是早还是晚.
我目前的代码是
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);
}
}
我试图将用户输入日期和当前日期都转换为两个Date对象,这样我就可以使用Date.compareTo()来简化比较日期.
我能够将用户输入字符串解析为Date对象.但是当前日期cal.getTime().toString()由于是无效字符串而不会解析为Date对象.
怎么去做这个?提前致谢