Search code examples
simpledateformatjava-6date

Compare string date with current date in Java 6


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


Solution

  • 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