I am trying to write out to excel and I have these dates. So is created a method that calculates the number of days from 01-01-1900. My problem is that I am two days off, so I adjusted the jan01 date two days.
Does anyone know why it is two days off? the only thing I can think of is that it is calculating the leap years incorrectly. every four years except century years, and there are two century years here (1900 and 2000),
public int dateAsInt() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse ( "1899-12-30" );
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
NSTimestamp jan011900 = new NSTimestamp(date);
System.out.println(jan011900);
long diff = eventDate().getTime() - jan011900.getTime(); // diff is in milliseconds
long roundingOffset = ( diff >= 0 ) ? 12 : -12; // if diff is positive, offset up, if negative, offset down
// next, convert diff to hours, offset by .5 days to round the end result, divide by 24 and truncate.
long days = ( ( diff / ( 1000 * 60 * 60 ) ) + roundingOffset ) / 24;
return (int)days;
}