Comparing user input date with current date

Hi, I am trying to compare a user entered date (as a string) with the current date to find out if this date was earlier or older.

My current code

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); } } 

I am trying to get both the input date and the current user date on two Date objects, so that I can use Date.compareTo () to simplify date comparison.

I was able to parse the user input string into a Date object. However, the current date is cal.getTime (). ToString () does not parse the Date object due to an invalid string.

How to do it? thanks in advance

+7
java date calendar simpledateformat
source share
5 answers

You can get the current Date with:

 todayDate = new Date(); 

EDIT: since you need to compare dates without considering the time component, I recommend you see this: How do I compare two dates without a time part?

Despite the "bad shape" of one answer, I really like it:

 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); sdf.format(date1).equals(sdf.format(date2)); 

In your case, you already have:

 SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy"); 

so I would consider (for simplicity, not performance):

 todayDate = dateFormatter.parse(dateFormatter.format(new Date() )); 
+6
source share

Here is the code to check if the given time-date is longer than the current time-time. The Boulow method takes the string perticular date-time as an argument and returns true if the provided date-time is greater than the current date-time . #thmharsh

 private boolean isExpire(String date){ if(date.isEmpty() || date.trim().equals("")){ return false; }else{ SimpleDateFormat sdf = new SimpleDateFormat("MMM-dd-yyyy hh:mm:ss a"); // Jan-20-2015 1:30:55 PM Date d=null; Date d1=null; String today= getToday("MMM-dd-yyyy hh:mm:ss a"); try { //System.out.println("expdate>> "+date); //System.out.println("today>> "+today+"\n\n"); d = sdf.parse(date); d1 = sdf.parse(today); if(d1.compareTo(d) <0){// not expired return false; }else if(d.compareTo(d1)==0){// both date are same if(d.getTime() < d1.getTime()){// not expired return false; }else if(d.getTime() == d1.getTime()){//expired return true; }else{//expired return true; } }else{//expired return true; } } catch (ParseException e) { e.printStackTrace(); return false; } } } public static String getToday(String format){ Date date = new Date(); return new SimpleDateFormat(format).format(date); } 
+3
source share

You can do it.

 // Make a Calendar whose DATE part is some time yesterday. Calendar cal = Calendar.getInstance(); cal.roll(Calendar.DATE, -1); if (myDate.before(cal.getTime())) { // myDate must be yesterday or earlier } else { // myDate must be today or later } 

It doesn't matter that cal has a time component, because myDate not. Therefore, when you compare them, if cal and myDate are the same date, the time component will make cal later than myDate , regardless of what the time component is.

+1
source share

Creating a new Date () will give you a Date object with the current date.

So:

  Date currentDate = new Date(); 

will complete the task

+1
source share

Fomat does not match as you expect.

  Calendar cal = Calendar.getInstance(); System.out.println(cal.getTime().toString()); 

Conclusion: Fri Nov 01 13:46:52 EET 2013

0
source share

All Articles