0

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Arvind Bakshi
  • 65
  • 1
  • 1
  • 9
  • If you want to check if a date is after another date you might want to try the `after(date)` method instead. – Roger Lindsjö Nov 09 '18 at 09:06
  • Possible duplicate of [SimpleDateFormat ignoring month when parsing](https://stackoverflow.com/questions/3056703/simpledateformat-ignoring-month-when-parsing) and/or [How can I format a String, which is a date, to a new date format in Java?](https://stackoverflow.com/questions/50937936/how-can-i-format-a-string-which-is-a-date-to-a-new-date-format-in-java) – Ole V.V. Nov 09 '18 at 09:11
  • @RogerLindsjö I fail to see any particular advantage of `after` over `before` here? – Ole V.V. Nov 09 '18 at 09:37
  • @OleV.V. I understood it as the OP said the check was incorrect for the same dates and thought "today" should also be included in the "Invalid Expiry Date" range. Missed that the problem wasn't actually same date, but rather marking months as minutes. So the "expDate is the current date" vas a false statement. – Roger Lindsjö Nov 09 '18 at 14:25

1 Answers1

0

java.time and ThreeTen Backport for Java 6

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.

Will that work on my Java 6?

Yes, java.time just requires at least Java 6 (I have tested the code above on Java 7).

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in. In this case import the classes from the java.timepackage with subpackages (not org.threeten.bp).
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

What went wrong in your code?

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.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Thanks for the mm comment. Replacing mm by MM made the code work. My code is running fine on java 6 with MM corrected. I have never used LocalDate that's why used Date. – Arvind Bakshi Nov 09 '18 at 09:53
  • These are imports import org.threeten.bp.LocalDate; import org.threeten.bp.ZoneId; import org.threeten.bp.format.DateTimeFormatter; don't work in my system. – Arvind Bakshi Nov 09 '18 at 09:56
  • Glad you found a solution. The imports won’t work until you add ThreeTen Backport as a dependency to your project. More information [here: Dependency Information](https://www.threeten.org/threetenbp/dependency-info.html). Or download the JAR file and drop it with your external JARs, all depending on your project setup. – Ole V.V. Nov 09 '18 at 10:40