Java Date -- again ...

  • Thread starter Sebastian Millies
  • Start date
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]>
 
S

Sebastian Millies

Am Mon, 9 Oct 2006 13:24:59 +0200 schrieb Sebastian Millies:
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;
^^
Found the mistake.
This line must read

long utcTime = ( ( dn + 1 ) * MILLISECONDS_PER_DAY ) - offset;

as I want the point in time when dn day have completely passed.
Hope this was the only mistake. Back to writing more tests ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Date/Calendar confusion 0
Date/Calendar confusion 8
Daylight Savings Time in Calendar 17
Date different 32
java Date problem 29
Timezones and versions of Java 25
Timezone/DST 5
java date issues 2

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top