Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #25419

Re: Searching for a "TimeInterval" type...

Date 2013-10-01 21:54 +0200
From Marcel Müller <news.5.maazl@spamgourmet.org>
Newsgroups comp.lang.java.programmer
Subject Re: Searching for a "TimeInterval" type...
References <slrnl4m1k3.mol.avl@gamma.logic.tuwien.ac.at>
Message-ID <524b2870$0$6547$9b4e6d93@newsspool4.arcor-online.net> (permalink)
Organization Arcor

Show all headers | View raw


On 01.10.13 19.29, Andreas Leitgeb wrote:
> My application will maintain Calendar instances, and I need another type
> to hold "intervals", such as (5 months, 4 days and 30 minutes) that I
> could conveniently "add" to a Calendar instance.

There is no such class in Java. The natural representation of time span 
is an integer, in doubt a long, holding he difference of Calendar.getTime().

However, there are some non-unique definitions of time spans in 
business. E.g. one month. It may mean anything between 28 and 31 days as 
well as an undefined time period, e.g. if you add one month to Jan. 30th.

> My first thought was using another Calendar, and do a loop for the
> add-operation:
>     Calendar cal = ...; Calendar intvl = ...;
>     for (int i=0; i<Calendar.FIELD_COUNT; i++) {
>        if (intvl.isSet(i)) { cal.add(i,intvl.get(i)); }
>     }
> which would be fine for simple cases, but wouldn't work, if I
> wanted to represent "100 days", which definitely isn't equivalent
> to "3 months and 10/9 days" (depending on leapyear).  If I just
> don't call "complete()", then according to the docs get() would
> bomb on me, for the days-value being out of bounds.

That's the problem. You first need a /complete/ definition what should 
happen, removing all undefined constellations.

> One alternative could be to just wrap a new int[Calendar.FIELD_COUNT],
> and use the Calendar-constants to index into the array, but I wonder,
> if I'm perhaps missing something more elegant.

You missed nothing. You do not even need so much fields. In general 
three fields should be sufficient with respect to the Gregorian 
Calendar. Years, months and everything else. Have a look at the oracle 
PL/SQL interval types.

However, for business this might sill be insufficient. There are terms 
like 'after three business days'. The result might be different from 
country to country.

> It's hard for me to
> believe, that there wasn't any common need for representing intervals
> of time in a mixture of units, and not just seconds.

There are many needs for time intervals. There is just no unique 
definition of the requirement.


Marcel

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Searching for a "TimeInterval" type... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-10-01 17:29 +0000
  Re: Searching for a "TimeInterval" type... markspace <markspace@nospam.nospam> - 2013-10-01 11:11 -0700
    Re: Searching for a "TimeInterval" type... markspace <markspace@nospam.nospam> - 2013-10-01 11:22 -0700
      Re: Searching for a "TimeInterval" type... Arne Vajhøj <arne@vajhoej.dk> - 2013-10-01 19:51 -0400
      Re: Searching for a "TimeInterval" type... Sebastian <news@seyweiler.dyndns.org> - 2013-10-02 09:23 +0200
        Re: Searching for a "TimeInterval" type... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-10-02 14:42 +0000
          Re: Searching for a "TimeInterval" type... Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-10-03 07:57 +0200
            Re: Searching for a "TimeInterval" type... Arne Vajhøj <arne@vajhoej.dk> - 2013-10-03 20:44 -0400
              Re: Searching for a "TimeInterval" type... Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2013-10-05 04:22 +0200
          Re: Searching for a "TimeInterval" type... Sebastian <news@seyweiler.dyndns.org> - 2013-10-16 19:43 +0200
  Re: Searching for a "TimeInterval" type... Marcel Müller <news.5.maazl@spamgourmet.org> - 2013-10-01 21:54 +0200
    Re: Searching for a "TimeInterval" type... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-10-02 15:02 +0000
  Re: Searching for a "TimeInterval" type... Silvio <silvio@internet.com> - 2013-10-01 22:04 +0200
  Re: Searching for a "TimeInterval" type... Arne Vajhøj <arne@vajhoej.dk> - 2013-10-01 19:53 -0400
    Re: Searching for a "TimeInterval" type... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-10-02 15:12 +0000
      Re: Searching for a "TimeInterval" type... Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-10-02 23:25 +0300
        Re: Searching for a "TimeInterval" type... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-10-02 23:56 +0000
          Re: Searching for a "TimeInterval" type... Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-10-03 17:18 +0300
            Re: Searching for a "TimeInterval" type... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-10-03 18:54 +0000
              Re: Searching for a "TimeInterval" type... Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-10-03 23:53 +0300
      Re: Searching for a "TimeInterval" type... Silvio <silvio@internet.com> - 2013-10-02 23:03 +0200
        Re: Searching for a "TimeInterval" type... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-10-03 00:04 +0000
      Re: Searching for a "TimeInterval" type... Arne Vajhøj <arne@vajhoej.dk> - 2013-10-02 21:36 -0400
        Re: Searching for a "TimeInterval" type... Arne Vajhøj <arne@vajhoej.dk> - 2013-10-03 21:41 -0400
          Re: Searching for a "TimeInterval" type... Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-10-04 09:17 +0000
            Re: Searching for a "TimeInterval" type... Arne Vajhøj <arne@vajhoej.dk> - 2013-10-04 20:52 -0400

csiph-web