Background: I am migrating an enterprise application using java.util.Date and java.sql.Date to joda-time DateTime. When unit testing in various timezones for equivalence of Joda functions to JDK there is a difference in Asia/Tehran zone.
Problem: The following methods are equivalent for most timezones tested after modern timezone was introduced
private Date addDaysToDateJoda(Date date, int days) {
Chronology chrono = GregorianChronology.getInstance();//hard set for test case simplicity
DateTimeZone dtz = DateTimeZone.forTimeZone(TimeZone.getDefault());
DateTime jodaDateTime = new DateTime(System.currentTimeMillis()).withChronology(chrono).withZone(dtz).withMillis(date.getTime());
return new Date(jodaDateTime.plusDays(days).getMillis());
}
private Date addDaysToDateJDK(Date date, int days) {
Calendar cal1 = new GregorianCalendar();
cal1.setTime(new Date(date.getTime()));
cal1.add(Calendar.DAY_OF_MONTH, days);
return new Date(cal1.getTime().getTime());
}
The Iran timezone (+3hr 30min) failed for all years whenever DST changes back to normal time offset in September/October.
JUnit Test Case