Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Java Date -- again ...

Thread view: 
Sebastian Millies - 09 Oct 2006 12:24 GMT
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]>
Sebastian Millies - 09 Oct 2006 13:24 GMT
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:
[quoted text clipped - 6 lines]
>     // 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 ...

>     cal.setTimeInMillis(utcTime);
>    
>     return cal;
>   }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.