Ok so I have 2 String variables holding dates in the format dd/mm/yyyy and I need to find the difference in days between them. Ideas?

String date1 = "dd/mm/yyyy";
String date2 = "dd/mm/yyyy";

int difference = (date1 - date2);

EDIT: and unless absolutely necessary, I dont want to use the Calendar package.

Edited by RockJake28: Extra information

5 Years
Discussion Span
Last Post by bguild

I would start with an enum of months that can translate numbers to months and gives each month a length, like:

enum Month { JANUARY(1,31), FEBRUARY(2,28) /* TODO: Other months */;
    private static Map<Integer,Month> monthNumbers = new HashMap<Integer,Month>();
    static {
        for(Month m: values())
            monthNumbers.put(m.number, m);
    public static Month forNumber(int number) { return monthNumbers.get(number); }
    private final int number;
    private final int length;
    private Month(int number, int length) {
        this.number = number;
        this.length = length;
    public int length() { return length; }

You'll want to create a method for deciding if a year is a leap-year so you can add one to February. Having those two tools should make it easier, but you probably don't want to iterate over every month between your two dates, or even over every year, so you should create a method that determines the number of leap-years in a range of years and add that to the number of years times 365.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.