Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {

    public static void main(String[] args) {
        String startDate = "14-08-2014";
        String endDate = "21/08/2020";

        SimpleDateFormat sdf[] = new SimpleDateFormat[] { new SimpleDateFormat("dd/MM/yyyy"),
                new SimpleDateFormat("dd-MM-yyyy") };

        Date dateStart = parse(startDate, sdf);
        Date dateEnd = parse(endDate, sdf);

        System.out.println("Is today bewteen " + dateStart + " and " + dateEnd);
        Date today = new Date();
        if (today.after(dateStart) && today.before(dateEnd)) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }

    public static Date parse(String value, DateFormat... formatters) {
        Date date = null;
        for (DateFormat formatter : formatters) {
            try {
                date = formatter.parse(value);
                break;
            } catch (ParseException e) {
            }
        }
        return date;
    }

}