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?
Eg.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.