Q:

How to create conditional statement before a specific year

I have a method that should process a string only if it is before a specific year. However I don't seem to be able to get the thing to work - I don't think the comparison is being done correctly. Can someone show me how the comparison is done for just years. If Joda time is better can you also show how (rather than tell me to just use joda time)
The string is in the UK date format eg 16/02/2006
My code:
DateFormat df = new SimpleDateFormat("yyyy"); 
Date yearOnReport = df.parse(startDateString);
Date threshold = df.parse("2006")
if (yearOnReport<threshold){
...\\Do some stuff
}

A:

In Java 8, use a LocalDate and a DateTimeFormatter:
String startDateString = "16/02/2006";

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu");
LocalDate localDate = LocalDate.parse(startDateString, dateTimeFormatter);
if (localDate.getYear() < 2006) {
    // code here
}

If you need to support older versions of Java, use Calendar and SimpleDateFormat:
String startDateString = "16/02/2006";

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = simpleDateFormat.parse(startDateString);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
if (calendar.get(Calendar.YEAR) < 2006) {
    // code here
}

In Java 7, you can do it the Java 8 way by getting the backport of the new java.time API from the ThreeTen project.
Advantage: Code will work when later upgrading to Java 8, without needing an extra library in Java 8.
Alternatively, add Joda-Time and use its LocalDate and DateTimeFormat:
String startDateString = "16/02/2006";

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy");
LocalDate localDate = formatter.parseLocalDate(startDateString);
if (localDate.getYear() < 2006) {
    // code here
}

