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
import org.threeten.bp.LocalDate;
import org.threeten.bp.ZoneId;
import org.threeten.bp.format.DateTimeFormatter;
public class ExpiryDateLogic {
public static void main(String[] args) {
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-uuuu");
ZoneId zone = ZoneId.of("Asia/Kolkata");
String expDateString = "07-11-2018"; // Text Date Input
if (expDateString.isEmpty()) {
System.out.println("No Expiry Date Present");
} else {
LocalDate expDate = LocalDate.parse(expDateString, dateFormatter);
LocalDate today = LocalDate.now(zone);
System.out.println("Today: " + today);
System.out.println("Expiration: " + expDate);
if (expDate.isBefore(today)) {
System.out.println("Invalid Expiry Date");
} else {
System.out.println("Valid Expiry Date");
}
}
}
}
Output when I run today (November 9):
Today: 2018-11-09 Expiration: 2018-11-07 Invalid Expiry Date
A LocalDate
is a date without time of day, so seems to match your requirement better than an old-fashioned Date
, which despite its name is a point in time, not a date. So the code above is also simpler than your code.
Since it is never the same date in all time zones I am specifying the time zone explicitly for predictable results.
Yes, java.time just requires at least Java 6 (I have tested the code above on Java 7).
java.time
package with subpackages (not org.threeten.bp
).org.threeten.bp
with subpackages.In your two formatters you are using mm
for month, that’s wrong. Lowercase mm
is for minute of the hour. For month you need to use uppercase MM
. All format pattern letters are case sensitive.
It’s typical for the SimpleDateFormat
class just to give you an incorrect result and pretend that all is well. It’s just one of the many troubles with that class. And one of the many reasons why I suggest using java.time, the modern Java date and time API, instead.
java.time
.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).