0

I am getting a hard coded date from the property file , which is of the format dd-MMM-yyyy.

Now i need to compare it with current date of same format. For that purpose , i cooked up this piece of code :

Date convDate = new Date();
Date currentFormattedDate = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
convDate = new SimpleDateFormat("dd-MMM-yyyy").parse("20-Aug-2013");
currentFormattedDate = new Date(dateFormat.format(currentFormattedDate)); 
if(currentFormattedDate.after(convDate) || currentFormattedDate.equals(convDate)){
  System.out.println("Correct");
}else{
  System.out.println("In correct");
}

But eclipse tells me that new Date has been depreciated. Does any one know of any alternative way of doing this ? I am going crazy over this. Thanks !

The Dark Knight
  • 5,455
  • 11
  • 54
  • 95

3 Answers3

3

One of the way is to use the Calendar class and its after() , equals() and before() methods.

Calendar currentDate = Calendar.getInstance();
Calendar anotherDate = Calendar.getInstance();
Date convDate = new SimpleDateFormat("dd-MMM-yyyy").parse("20-Aug-2013");
anotherDate.setTime(convDate);
if(currentDate .after(anotherDate) || 
   currentDate .equals(anotherDate)){
    System.out.println("Correct");
}else{
   System.out.println("In correct");
}

You can also use Jodatime library , see this SO answer.

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
1

You should use the Date(long) constructor:

Date convDate = new Date(System.currentTimeMillis());

This way you'll avoid the deprecation warning and will get a Date instance with the system time.

Tomas Narros
  • 13,390
  • 2
  • 40
  • 56
1

Date represents number of milliseconds since the epoch. Why not just use the Date returned from

Date currentFormattedDate = new Date();

?

Reimeus
  • 158,255
  • 15
  • 216
  • 276