This is a program that compares the input string date(expdate) with the current date(today) and returns "valid Expiry Date" only if expDate is greater than current date.
import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/* Name of the class has to be "Main" only if the class is public. */
class expiryDateLogic
{
public static void main (String[] args) throws java.lang.Exception
{
String expdate = "07-11-2018"; // Text Date Input
if (!expdate.equals("")) { // If null no checking
DateFormat format = new SimpleDateFormat("dd-mm-yyyy");
Date expDate = (Date) format.parse(expdate); // Convert expdate to type Date
SimpleDateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
Date current = new Date();
String td = formatter.format(current);
Date today = (Date) formatter.parse(td); // Current date
System.out.println(today);
System.out.println(expDate);
// System.out.println(expDate.compareTo(today));
if (expDate.before(today)) { // Date Comparison
System.out.println("Invalid Expiry Date");
} else {
System.out.println("Valid Expiry Date");
}
} else {
System.out.println("No Expiry Date Present");
}
}
}
This code doesn't work if expDate is the current date. Please Help