S
Sebastian Millies
This has been discussed to death, but I still can't get my head around it.
I want to compute a day number from a date and then get back the date.
Could anyone please point out where I'm going wrong? I guess I'm
misunderstanding something about Calendar offsets, or about the
GregorianCalendar constructors.
-- Thanks, Sebastian
Here's how I compute the number of days that have passed in local time
since Jan 1, 1970. (seems to work fine):
public class MyCalendar extends GregorianCalendar
{
private static final long MILLISECONDS_PER_DAY = 86400000L;
public MyCalendar() {
super();
}
public MyCalendar(int year, int month, int day) {
super(year, month, day);
}
public long getDayNumber() {
// adjust from utc to local time adding the Calendar offset values
long offset = get( Calendar.ZONE_OFFSET ) + get( Calendar.DST_OFFSET );
long day = ( long )Math.floor( ( double )( getTimeInMillis() + offset )
/ ( ( double )MILLISECONDS_PER_DAY ) );
return day;
}
}
However, the inverse method (creating a Calendar from a day number)
doesn't work as I expect:
public static MyCalendar createInstanceDayNumber(long dn)
{
MyCalendar cal = new MyCalendar();
long offset = cal.get( Calendar.ZONE_OFFSET )
+ cal.get( Calendar.DST_OFFSET );
// adjust from local time to utc subtracting the Calendar offset values
long utcTime = ( dn * MILLISECONDS_PER_DAY ) - offset;
cal.setTimeInMillis(utcTime);
return cal;
}
With this Unit Test:
@Test
public void testCreateInstanceDayNumber()
{
SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy");
MyCalendar epoch = new MyCalendar(1970, Calendar.JANUARY, 1);
MyCalendar cal = MyCalendar.createInstanceDayNumber(0);
assertEquals(df.format(epoch.getTime()), df.format(cal.getTime()));
}
org.junit.ComparisonFailure: null expected:<[01.01.1970]> but
was:<[31.12.1969]>
I want to compute a day number from a date and then get back the date.
Could anyone please point out where I'm going wrong? I guess I'm
misunderstanding something about Calendar offsets, or about the
GregorianCalendar constructors.
-- Thanks, Sebastian
Here's how I compute the number of days that have passed in local time
since Jan 1, 1970. (seems to work fine):
public class MyCalendar extends GregorianCalendar
{
private static final long MILLISECONDS_PER_DAY = 86400000L;
public MyCalendar() {
super();
}
public MyCalendar(int year, int month, int day) {
super(year, month, day);
}
public long getDayNumber() {
// adjust from utc to local time adding the Calendar offset values
long offset = get( Calendar.ZONE_OFFSET ) + get( Calendar.DST_OFFSET );
long day = ( long )Math.floor( ( double )( getTimeInMillis() + offset )
/ ( ( double )MILLISECONDS_PER_DAY ) );
return day;
}
}
However, the inverse method (creating a Calendar from a day number)
doesn't work as I expect:
public static MyCalendar createInstanceDayNumber(long dn)
{
MyCalendar cal = new MyCalendar();
long offset = cal.get( Calendar.ZONE_OFFSET )
+ cal.get( Calendar.DST_OFFSET );
// adjust from local time to utc subtracting the Calendar offset values
long utcTime = ( dn * MILLISECONDS_PER_DAY ) - offset;
cal.setTimeInMillis(utcTime);
return cal;
}
With this Unit Test:
@Test
public void testCreateInstanceDayNumber()
{
SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy");
MyCalendar epoch = new MyCalendar(1970, Calendar.JANUARY, 1);
MyCalendar cal = MyCalendar.createInstanceDayNumber(0);
assertEquals(df.format(epoch.getTime()), df.format(cal.getTime()));
}
org.junit.ComparisonFailure: null expected:<[01.01.1970]> but
was:<[31.12.1969]>