Misunderstanding Period Class.

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Misunderstanding Period Class.

Elam Daly
Hi all,

I'm trying this simple example to understand the Period class. 

Why does the getDays() method return 0, instead of 472?

Thanks,
- Elam

DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
DateTime end = new DateTime(2006, 1, 1, 0, 0, 0, 0);

// period of 1 year and 7 days
Period period = new Period(start, end);
System.out.println(period.getDays()); // Returns 0


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Misunderstanding Period Class.

Matt Hughes
Because the getXXX methods just return that FIELD of time.  If you want the computed time, you need the toStandardXXX method.

On Tue, Sep 30, 2008 at 9:41 AM, Elam Daly <[hidden email]> wrote:
Hi all,

I'm trying this simple example to understand the Period class. 

Why does the getDays() method return 0, instead of 472?

Thanks,
- Elam

DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
DateTime end = new DateTime(2006, 1, 1, 0, 0, 0, 0);

// period of 1 year and 7 days
Period period = new Period(start, end);
System.out.println(period.getDays()); // Returns 0


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Misunderstanding Period Class.

Elam Daly
Well, using this particular example, all the toStandardXXX methods are throwing exceptions because of the presence of years in the Period, as the Javadocs state.  So is there a way around this, ie to convert a period of months/years into minutes/hours etc?

Thanks,
- Elam

On Tue, Sep 30, 2008 at 10:36 AM, Matt Hughes <[hidden email]> wrote:
Because the getXXX methods just return that FIELD of time.  If you want the computed time, you need the toStandardXXX method.

On Tue, Sep 30, 2008 at 9:41 AM, Elam Daly <[hidden email]> wrote:
Hi all,

I'm trying this simple example to understand the Period class. 

Why does the getDays() method return 0, instead of 472?

Thanks,
- Elam

DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
DateTime end = new DateTime(2006, 1, 1, 0, 0, 0, 0);

// period of 1 year and 7 days
Period period = new Period(start, end);
System.out.println(period.getDays()); // Returns 0


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Misunderstanding Period Class.

Elam Daly
Actually, what I'm trying to do is, given two DateTime instances, I need to calculate the number of minutes/hours/seconds etc between the two.  After looking over the mailing list, it looks like I can simply use the XXX.xxxBetween(DateTime one, DateTime two) methods.

Is this a reasonable solution?

Thanks,
- Elam

On Tue, Sep 30, 2008 at 10:50 AM, Elam Daly <[hidden email]> wrote:
Well, using this particular example, all the toStandardXXX methods are throwing exceptions because of the presence of years in the Period, as the Javadocs state.  So is there a way around this, ie to convert a period of months/years into minutes/hours etc?

Thanks,
- Elam


On Tue, Sep 30, 2008 at 10:36 AM, Matt Hughes <[hidden email]> wrote:
Because the getXXX methods just return that FIELD of time.  If you want the computed time, you need the toStandardXXX method.

On Tue, Sep 30, 2008 at 9:41 AM, Elam Daly <[hidden email]> wrote:
Hi all,

I'm trying this simple example to understand the Period class. 

Why does the getDays() method return 0, instead of 472?

Thanks,
- Elam

DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
DateTime end = new DateTime(2006, 1, 1, 0, 0, 0, 0);

// period of 1 year and 7 days
Period period = new Period(start, end);
System.out.println(period.getDays()); // Returns 0


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest




-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Misunderstanding Period Class.

HotMail-3
In reply to this post by Elam Daly

Try using the Period constructor that accepts a PeriodType.

 

    /**

     * Creates a period from the given interval endpoints.

     *

     * @param startInstant  interval start, in milliseconds

     * @param endInstant  interval end, in milliseconds

     * @param type  which set of fields this period supports, null means standard

     */

    public Period(long startInstant, long endInstant, PeriodType type)

 

 

Bryan

 

From: Elam Daly [mailto:[hidden email]]
Sent: Tuesday, September 30, 2008 9:51 AM
To: Discussion of the Joda project
Subject: Re: [Joda-interest] Misunderstanding Period Class.

 

Well, using this particular example, all the toStandardXXX methods are throwing exceptions because of the presence of years in the Period, as the Javadocs state.  So is there a way around this, ie to convert a period of months/years into minutes/hours etc?

Thanks,
- Elam

On Tue, Sep 30, 2008 at 10:36 AM, Matt Hughes <[hidden email]> wrote:

Because the getXXX methods just return that FIELD of time.  If you want the computed time, you need the toStandardXXX method.

On Tue, Sep 30, 2008 at 9:41 AM, Elam Daly <[hidden email]> wrote:

Hi all,

I'm trying this simple example to understand the Period class. 

Why does the getDays() method return 0, instead of 472?

Thanks,
- Elam

DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
DateTime end = new DateTime(2006, 1, 1, 0, 0, 0, 0);

// period of 1 year and 7 days
Period period = new Period(start, end);
System.out.println(period.getDays()); // Returns 0

 

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest

 


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest

 


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Misunderstanding Period Class.

Craig P. Motlin
In reply to this post by Elam Daly
DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
DateTime end = new DateTime(2006, 1, 1, 0, 0, 0, 0);

Interval interval = new Interval(start, end);
System.out.println(Days.
daysIn(interval).getDays()); // prints 372

On Tue, Sep 30, 2008 at 10:50 AM, Elam Daly <[hidden email]> wrote:
Well, using this particular example, all the toStandardXXX methods are throwing exceptions because of the presence of years in the Period, as the Javadocs state.  So is there a way around this, ie to convert a period of months/years into minutes/hours etc?

Thanks,
- Elam


On Tue, Sep 30, 2008 at 10:36 AM, Matt Hughes <[hidden email]> wrote:
Because the getXXX methods just return that FIELD of time.  If you want the computed time, you need the toStandardXXX method.

On Tue, Sep 30, 2008 at 9:41 AM, Elam Daly <[hidden email]> wrote:
Hi all,

I'm trying this simple example to understand the Period class. 

Why does the getDays() method return 0, instead of 472?

Thanks,
- Elam

DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
DateTime end = new DateTime(2006, 1, 1, 0, 0, 0, 0);

// period of 1 year and 7 days
Period period = new Period(start, end);
System.out.println(period.getDays()); // Returns 0


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Misunderstanding Period Class.

bart zagers
In reply to this post by Elam Daly

The Period class divides the given period over its different fields (years, months, weeks...). So your period will return 1 for both the getYears and the getWeeks method as the given period of between start and end date exist of exactly one year and one week. Therefore the getDays method will return zero. Had the given period been one day longer, the getDays method would have returned 1.
The toStandardXXX have their limitations because the Period only contains the fields and not the exact start and end dates. Therefore it can not correctly convert for example a year into a number of days (365 or 366?).

What you want is using the "SingleFieldPeriods" classes Years, Months... and in this case Days directly. 

DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
DateTime end = new DateTime(2006, 1, 1, 0, 0, 0, 0);

Days days = Days.daysBetween(start, end);
System.out.println(days.getDays());

This code will give you the expected result.

Bart



On Tue, Sep 30, 2008 at 3:41 PM, Elam Daly <[hidden email]> wrote:
Hi all,

I'm trying this simple example to understand the Period class. 

Why does the getDays() method return 0, instead of 472?

Thanks,
- Elam

DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
DateTime end = new DateTime(2006, 1, 1, 0, 0, 0, 0);

// period of 1 year and 7 days
Period period = new Period(start, end);
System.out.println(period.getDays()); // Returns 0


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Misunderstanding Period Class.

jodastephen
In reply to this post by Elam Daly
You need to write one of the following:

Period period = new Period(start, end, PeriodType.days());
int days = period.getDays();

Days period = Days.daysBetween(start, end);

Stephen



Elam Daly wrote:

> Hi all,
>
> I'm trying this simple example to understand the Period class.
>
> Why does the getDays() method return 0, instead of 472?
>
> Thanks,
> - Elam
>
> DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
> DateTime end = new DateTime(2006, 1, 1, 0, 0, 0, 0);
>
> // period of 1 year and 7 days
> Period period = new Period(start, end);
> System.out.println(period.getDays()); // Returns 0
>
>
> ------------------------------------------------------------------------
>
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
> Build the coolest Linux based applications with Moblin SDK & win great prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Misunderstanding Period Class.

jodastephen
In reply to this post by Elam Daly
Use
Period period = new Period(start, end, PeriodType.days());

Stephen


2008/9/30 Elam Daly <[hidden email]>:

> Hi all,
>
> I'm trying this simple example to understand the Period class.
>
> Why does the getDays() method return 0, instead of 472?
>
> Thanks,
> - Elam
>
> DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
> DateTime end = new DateTime(2006, 1, 1, 0, 0, 0, 0);
>
> // period of 1 year and 7 days
> Period period = new Period(start, end);
> System.out.println(period.getDays()); // Returns 0
>
>
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
> Build the coolest Linux based applications with Moblin SDK & win great
> prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest
>
>

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Loading...