🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.java.programmer
8 messages
8 total messages Started by Dag Welinder Thu, 30 Jul 1998 00:00
Days between GregorianCalendars
#426
Author: Dag Welinder
Date: Thu, 30 Jul 1998 00:00
7 lines
915 bytes
Hello!

Is it possible to compute the number of days between two dates? I have
two instances of GregorianCalendar, each containing a date. Can I
compute the number of days between these two dates?

Regards, Dag Welinder
Re: Days between GregorianCalendars
#422
Author: Ricky
Date: Thu, 30 Jul 1998 00:00
25 lines
1382 bytes
This should help a bit

int days = 0;

                while(startDate.get(Calendar.YEAR) !=
endDate.get(Calendar.YEAR) ||
                      startDate.get(Calendar.MONTH) !=
endDate.get(Calendar.MONTH) ||
                      startDate.get(Calendar.DATE) !=
endDate.get(Calendar.DATE) )
                {
                      startDate.add(Calendar.DATE, 1);
                      days++;
                }


Dag Welinder wrote:

> Hello!
>
> Is it possible to compute the number of days between two dates? I have
> two instances of GregorianCalendar, each containing a date. Can I
> compute the number of days between these two dates?
>
> Regards, Dag Welinder
Re: Days between GregorianCalendars
#427
Author: Marko Teittinen
Date: Thu, 30 Jul 1998 00:00
20 lines
1537 bytes
On Thu, 30 Jul 1998, Dag Welinder <welinder@bigfoot.com> wrote:

> Is it possible to compute the number of days between two dates? I have
> two instances of GregorianCalendar, each containing a date. Can I
> compute the number of days between these two dates?

1) Get the long integer value from each of the dates (which
   represents the time in milliseconds since a fixed time, 1 Jan
   1970, I think, but it's not important).

2) Subtract the smaller from the larger number and you get the
   difference between the two dates in milliseconds.

3) Divide by 1000*60*60*24 (number of milliseconds in a day) to get
   number of days between the dates.

-- 
Marko Teittinen (marko@tekamah.com)   | Please don't send me any
Human-Computer Interaction Specialist | spam. My mail filter kills
Tekamah Corporation                   | all incoming ads anyway.
Re: Days between GregorianCalendars
#428
Author: Thanos Vassilakis
Date: Thu, 30 Jul 1998 00:00
11 lines
1120 bytes
Try posing this message to the www.algorithms.org site

Dag Welinder wrote:

> Hello!
>
> Is it possible to compute the number of days between two dates? I have
> two instances of GregorianCalendar, each containing a date. Can I
> compute the number of days between these two dates?
>
> Regards, Dag Welinder
Re: Days between GregorianCalendars
#423
Author: Ralph Brown
Date: Fri, 31 Jul 1998 00:00
7 lines
1009 bytes
How about just doing a getTime() on the start and end and then divide the
difference by the number of milliseconds in a day?

This doesn't account for leap seconds, but I don't believe the Calendar
classes do either.

Ralph
Re: Days between GregorianCalendars
#424
Author: cameron_cp
Date: Fri, 31 Jul 1998 00:00
45 lines
2233 bytes
In article <35C07780.DF0F02D4@bigfoot.com>,
  Dag Welinder <welinder@bigfoot.com> wrote:
> Hello!
>
> Is it possible to compute the number of days between two dates? I have
> two instances of GregorianCalendar, each containing a date. Can I
> compute the number of days between these two dates?

  Translate the dates to Julian numbers and subtract the two.

  // Quick and Dirty translation from an older
  // turbo pascal function
  // UNTESTED - see original funciton

  int Julian(int M, int D,int Y) {
    int j2; int ju;
    j2 = (M - 14) / 12;
    ju = D - 32075 + (1461 * (Y + 4800 + j2) / 4);
    ju = ju + (367 * (M - 2 - j2 * 12) / 12);
    ju = ju - (3 * ( (Y + 4900 + j2) / 100) / 4);

    return ju;
  }

  // original function
  function dateType.Julian( mo, da, yr: integer): LongInt;
  var     i, j, k, j2, ju: longInt;
  begin
       i := yr;     j := mo;     k := da;
       j2 := ( (j - 14) div 12 );
       ju := k - 32075 + (1461 * ( i + 4800 + j2 ) div 4 );
       ju := ju + ( 367 * (j - 2 - j2 * 12) div 12);
       ju := ju - (3 * ( (i + 4900 + j2) div 100) div 4);
       Julian := ju;
  end;  { Jul }



--
The Idiot's Guide To Everything (ComputerRelated)
http://members.tripod.com/~igte
=================================================

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum
Re: Days between GregorianCalendars
#425
Author: Allan Wax
Date: Tue, 04 Aug 1998 00:00
22 lines
1415 bytes
Dag Welinder wrote:
> 
> Hello!
> 
> Is it possible to compute the number of days between two dates? I have
> two instances of GregorianCalendar, each containing a date. Can I
> compute the number of days between these two dates?
> 
> Regards, Dag Welinder

Get ACM's Collected Algorithm 199, recode it in Java (I did, took about
30 minutes), compute the Julian date for each end point, subtract the
two numbers, viola!
-- 
<------------------------------------------->
Allan Wax
e-mail:  Allan.Wax@jpl.nasa.gov
FAX:     + (213) 935-9292

NOTE: The views expressed in this message are
      my own and do not necessarily reflect
      those of my employer.
Re: Days between GregorianCalendars
#421
Author: William D. Scheidegger Jr
Date: Wed, 05 Aug 1998 00:00
28 lines
1647 bytes
Why not use the built in method:
    int julian = myCalendar.get(Calendar.DAY_OF_YEAR);
Bill Scheidegger

Allan Wax wrote:

> Dag Welinder wrote:
> >
> > Hello!
> >
> > Is it possible to compute the number of days between two dates? I have
> > two instances of GregorianCalendar, each containing a date. Can I
> > compute the number of days between these two dates?
> >
> > Regards, Dag Welinder
>
> Get ACM's Collected Algorithm 199, recode it in Java (I did, took about
> 30 minutes), compute the Julian date for each end point, subtract the
> two numbers, viola!
> --
> <------------------------------------------->
> Allan Wax
> e-mail:  Allan.Wax@jpl.nasa.gov
> FAX:     + (213) 935-9292
>
> NOTE: The views expressed in this message are
>       my own and do not necessarily reflect
>       those of my employer.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads