You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Travis Reeder (JIRA)" <ji...@apache.org> on 2008/04/30 00:19:55 UTC

[jira] Created: (LANG-434) Add DateUtils.ceiling() method

Add DateUtils.ceiling() method
------------------------------

                 Key: LANG-434
                 URL: https://issues.apache.org/jira/browse/LANG-434
             Project: Commons Lang
          Issue Type: Improvement
    Affects Versions: 2.4
            Reporter: Travis Reeder


It would be nice to have a DateUtils.ceiling methods which would be the opposite of truncate (which is equivalent to floor)

DateUtils.ceiling(Date d, int field);

For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:59:59.999. If this was passed with MONTH, it would return 31 Mar 2002 23:59:59.999.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (LANG-434) Add DateUtils.ceiling() method

Posted by "Emmanuel Bourg (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-434?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12594110#action_12594110 ] 

Emmanuel Bourg commented on LANG-434:
-------------------------------------

I might be interested by a similar method taking an additional parameter, a "multiplicator" relative to the unit selected. For example :

ceil('13:47:23', MINUTE, 5) -> 13:50
ceil('May 5', MONTH, 2) -> June 1st




> Add DateUtils.ceiling() method
> ------------------------------
>
>                 Key: LANG-434
>                 URL: https://issues.apache.org/jira/browse/LANG-434
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 2.4
>            Reporter: Travis Reeder
>             Fix For: 3.0
>
>
> It would be nice to have a DateUtils.ceiling methods which would be the opposite of truncate (which is equivalent to floor)
> DateUtils.ceiling(Date d, int field);
> For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:59:59.999. If this was passed with MONTH, it would return 31 Mar 2002 23:59:59.999.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (LANG-434) Add DateUtils.ceiling() method

Posted by "Robert Scholte (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-434?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12594537#action_12594537 ] 

Robert Scholte commented on LANG-434:
-------------------------------------

I've had a look at the source, and it looks to me we need to use the private method modify since it has a fix for [http://issues.apache.org/jira/browse/LANG-59], but then it's signature has to be changed. 

{code}
    /**
     * <p>Internal calculation method.</p>
     * 
     * @param val  the calendar
     * @param field  the field constant
     * @param round  true to round, false to truncate
     * @throws ArithmeticException if the year is over 280 million
     */
private static void modify(Calendar val, int field, boolean round)
{code}

Just a suggestion:
{code}
    /**
     * <p>Internal calculation method.</p>
     * 
     * @param val  the calendar
     * @param field  the field constant
     * @param round  -1 to floor, 0 to round, 1 to ceil
     * @throws ArithmeticException if the year is over 280 million
     */
private static void modify(Calendar val, int field, int round)
{code}



> Add DateUtils.ceiling() method
> ------------------------------
>
>                 Key: LANG-434
>                 URL: https://issues.apache.org/jira/browse/LANG-434
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 2.4
>            Reporter: Travis Reeder
>             Fix For: 3.0
>
>
> It would be nice to have a DateUtils.ceiling methods which would be the opposite of truncate (which is equivalent to floor)
> DateUtils.ceiling(Date d, int field);
> For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:59:59.999. If this was passed with MONTH, it would return 31 Mar 2002 23:59:59.999.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (LANG-434) Add DateUtils.ceiling() method

Posted by "Niall Pemberton (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-434?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Niall Pemberton updated LANG-434:
---------------------------------

    Fix Version/s:     (was: 3.0)
                   2.5

> Add DateUtils.ceiling() method
> ------------------------------
>
>                 Key: LANG-434
>                 URL: https://issues.apache.org/jira/browse/LANG-434
>             Project: Commons Lang
>          Issue Type: Improvement
>          Components: lang.time.*
>    Affects Versions: 2.4
>            Reporter: Travis Reeder
>             Fix For: 2.5
>
>         Attachments: LANG-434-rs.patch
>
>
> It would be nice to have a DateUtils.ceiling methods which would be the opposite of truncate (which is equivalent to floor)
> DateUtils.ceiling(Date d, int field);
> For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:59:59.999. If this was passed with MONTH, it would return 31 Mar 2002 23:59:59.999.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (LANG-434) Add DateUtils.ceiling() method

Posted by "Serge Knystautas (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-434?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12601249#action_12601249 ] 

Serge Knystautas commented on LANG-434:
---------------------------------------

I tried to reply to Robert's email but it bounced, so posting reply here.

I believe [Robert Scholte's] interpretation sounds correct. I would expect the assert comments to be what should be tested, not the code. In other words, those tests should work if the method was truncate and should be failing if using the round method.

> Add DateUtils.ceiling() method
> ------------------------------
>
>                 Key: LANG-434
>                 URL: https://issues.apache.org/jira/browse/LANG-434
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 2.4
>            Reporter: Travis Reeder
>             Fix For: 3.0
>
>
> It would be nice to have a DateUtils.ceiling methods which would be the opposite of truncate (which is equivalent to floor)
> DateUtils.ceiling(Date d, int field);
> For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:59:59.999. If this was passed with MONTH, it would return 31 Mar 2002 23:59:59.999.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (LANG-434) Add DateUtils.ceiling() method

Posted by "Travis Reeder (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-434?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12594434#action_12594434 ] 

Travis Reeder commented on LANG-434:
------------------------------------

Not a patch, but this should work as the equivalent of the Math.ceil().

 public Date ciel(Date d, int field) {
        d = DateUtils.add(d, field, +1);
        d = DateUtils.truncate(d, field);
        return d;
    }

Quick visual test:

 Date d;
        d = new Date();
        System.out.println("now=" + d);
        System.out.println("ciel=" + ciel(d, Calendar.YEAR));
        System.out.println("ciel=" + ciel(d, Calendar.MONTH));
        System.out.println("ciel=" + ciel(d, Calendar.DAY_OF_MONTH));
        System.out.println("ciel=" + ciel(d, Calendar.HOUR));
        System.out.println("ciel=" + ciel(d, Calendar.MINUTE));



> Add DateUtils.ceiling() method
> ------------------------------
>
>                 Key: LANG-434
>                 URL: https://issues.apache.org/jira/browse/LANG-434
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 2.4
>            Reporter: Travis Reeder
>             Fix For: 3.0
>
>
> It would be nice to have a DateUtils.ceiling methods which would be the opposite of truncate (which is equivalent to floor)
> DateUtils.ceiling(Date d, int field);
> For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:59:59.999. If this was passed with MONTH, it would return 31 Mar 2002 23:59:59.999.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (LANG-434) Add DateUtils.ceiling() method

Posted by "Robert Scholte (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-434?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12593821#action_12593821 ] 

Robert Scholte commented on LANG-434:
-------------------------------------

If you follow java's Math-api, it would be DateUtils.ceil(Date d, int field) and not ceiling.
And ceil would give you the next full value, which means you need to add that last millisecond.

Following your examples:
if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 14:00:00.000. 
If this was passed with MONTH, it would return 1 Apr 2002 00:00:00.000.



> Add DateUtils.ceiling() method
> ------------------------------
>
>                 Key: LANG-434
>                 URL: https://issues.apache.org/jira/browse/LANG-434
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 2.4
>            Reporter: Travis Reeder
>
> It would be nice to have a DateUtils.ceiling methods which would be the opposite of truncate (which is equivalent to floor)
> DateUtils.ceiling(Date d, int field);
> For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:59:59.999. If this was passed with MONTH, it would return 31 Mar 2002 23:59:59.999.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Closed: (LANG-434) Add DateUtils.ceiling() method

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-434?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell closed LANG-434.
------------------------------

    Resolution: Fixed

Patch applied. Thanks Robert and Travis.

I renamed ceil to ceiling as we don't call truncate floor or trunc. Seems to fit better.

> Add DateUtils.ceiling() method
> ------------------------------
>
>                 Key: LANG-434
>                 URL: https://issues.apache.org/jira/browse/LANG-434
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 2.4
>            Reporter: Travis Reeder
>             Fix For: 3.0
>
>         Attachments: LANG-434-rs.patch
>
>
> It would be nice to have a DateUtils.ceiling methods which would be the opposite of truncate (which is equivalent to floor)
> DateUtils.ceiling(Date d, int field);
> For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:59:59.999. If this was passed with MONTH, it would return 31 Mar 2002 23:59:59.999.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (LANG-434) Add DateUtils.ceiling() method

Posted by "Robert Scholte (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-434?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12600550#action_12600550 ] 

Robert Scholte commented on LANG-434:
-------------------------------------

I've started creating a patch and wrote some tests based on the truncate and round-tests, but I ran into something strange. Watch the following sniplets:
{code:title=DateUtilsTest.java}
protected void setUp() throws Exception {

dateAmPm1 = dateTimeParser.parse("February 3, 2002 01:10:00.000");
dateAmPm2 = dateTimeParser.parse("February 3, 2002 11:10:00.000");
dateAmPm3 = dateTimeParser.parse("February 3, 2002 13:10:00.000");
dateAmPm4 = dateTimeParser.parse("February 3, 2002 19:10:00.000");

}
{code}
and
{code:title=DateUtilsTest.java}
public void testRound() throws Exception {
assertEquals("truncate ampm-1 failed",
                dateTimeParser.parse("February 3, 2002 00:00:00.000"),
                DateUtils.round(dateAmPm1, Calendar.AM_PM));
assertEquals("truncate ampm-2 failed",
                dateTimeParser.parse("February 4, 2002 00:00:00.000"),
                DateUtils.round(dateAmPm2, Calendar.AM_PM));
assertEquals("truncate ampm-3 failed",
                dateTimeParser.parse("February 3, 2002 12:00:00.000"),
                DateUtils.round(dateAmPm3, Calendar.AM_PM));
assertEquals("truncate ampm-4 failed",
                dateTimeParser.parse("February 4, 2002 12:00:00.000"),
                DateUtils.round(dateAmPm4, Calendar.AM_PM));
}
{code}

Minor issue: the comment has to be round instead of truncate
But I'd expected the values round to the nearest noon or midnight. In other words:

ampm-2 as "February 3, 2002 12:00:00.000"
ampm-4 as "February 4, 2002 00:00:00.000"

Could someone explain to me what's going on?



> Add DateUtils.ceiling() method
> ------------------------------
>
>                 Key: LANG-434
>                 URL: https://issues.apache.org/jira/browse/LANG-434
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 2.4
>            Reporter: Travis Reeder
>             Fix For: 3.0
>
>
> It would be nice to have a DateUtils.ceiling methods which would be the opposite of truncate (which is equivalent to floor)
> DateUtils.ceiling(Date d, int field);
> For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:59:59.999. If this was passed with MONTH, it would return 31 Mar 2002 23:59:59.999.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (LANG-434) Add DateUtils.ceiling() method

Posted by "Paul Benedict (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-434?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12855836#action_12855836 ] 

Paul Benedict commented on LANG-434:
------------------------------------

I think the method name ceiling() is 100% misleading.

> Add DateUtils.ceiling() method
> ------------------------------
>
>                 Key: LANG-434
>                 URL: https://issues.apache.org/jira/browse/LANG-434
>             Project: Commons Lang
>          Issue Type: Improvement
>          Components: lang.time.*
>    Affects Versions: 2.4
>            Reporter: Travis Reeder
>             Fix For: 2.5
>
>         Attachments: LANG-434-rs.patch
>
>
> It would be nice to have a DateUtils.ceiling methods which would be the opposite of truncate (which is equivalent to floor)
> DateUtils.ceiling(Date d, int field);
> For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:59:59.999. If this was passed with MONTH, it would return 31 Mar 2002 23:59:59.999.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] Commented: (LANG-434) Add DateUtils.ceiling() method

Posted by "Rodrigo Bartels (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-434?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12855795#action_12855795 ] 

Rodrigo Bartels commented on LANG-434:
--------------------------------------

Javadoc about the ceiling methods is wrong...

Check http://commons.apache.org/lang/api-release/org/apache/commons/lang/time/DateUtils.html#ceiling(java.util.Calendar, int)

It seems that someone copy the round method comments...

> Add DateUtils.ceiling() method
> ------------------------------
>
>                 Key: LANG-434
>                 URL: https://issues.apache.org/jira/browse/LANG-434
>             Project: Commons Lang
>          Issue Type: Improvement
>          Components: lang.time.*
>    Affects Versions: 2.4
>            Reporter: Travis Reeder
>             Fix For: 2.5
>
>         Attachments: LANG-434-rs.patch
>
>
> It would be nice to have a DateUtils.ceiling methods which would be the opposite of truncate (which is equivalent to floor)
> DateUtils.ceiling(Date d, int field);
> For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:59:59.999. If this was passed with MONTH, it would return 31 Mar 2002 23:59:59.999.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] Updated: (LANG-434) Add DateUtils.ceiling() method

Posted by "Henri Yandell (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-434?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Yandell updated LANG-434:
-------------------------------

    Fix Version/s: 3.0

Seems good to me. Patches welcome :)

> Add DateUtils.ceiling() method
> ------------------------------
>
>                 Key: LANG-434
>                 URL: https://issues.apache.org/jira/browse/LANG-434
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 2.4
>            Reporter: Travis Reeder
>             Fix For: 3.0
>
>
> It would be nice to have a DateUtils.ceiling methods which would be the opposite of truncate (which is equivalent to floor)
> DateUtils.ceiling(Date d, int field);
> For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:59:59.999. If this was passed with MONTH, it would return 31 Mar 2002 23:59:59.999.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (LANG-434) Add DateUtils.ceiling() method

Posted by "Robert Scholte (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/LANG-434?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Robert Scholte updated LANG-434:
--------------------------------

    Attachment: LANG-434-rs.patch

issue 440 was committed. Now the tests for this issue work fine. Added both ceil-method and it's tests

> Add DateUtils.ceiling() method
> ------------------------------
>
>                 Key: LANG-434
>                 URL: https://issues.apache.org/jira/browse/LANG-434
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 2.4
>            Reporter: Travis Reeder
>             Fix For: 3.0
>
>         Attachments: LANG-434-rs.patch
>
>
> It would be nice to have a DateUtils.ceiling methods which would be the opposite of truncate (which is equivalent to floor)
> DateUtils.ceiling(Date d, int field);
> For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:59:59.999. If this was passed with MONTH, it would return 31 Mar 2002 23:59:59.999.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Issue Comment Edited: (LANG-434) Add DateUtils.ceiling() method

Posted by "Travis Reeder (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/LANG-434?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12594434#action_12594434 ] 

prophecy edited comment on LANG-434 at 5/5/08 6:26 PM:
------------------------------------------------------------

Not a patch, but this should work as the equivalent of the Math.ceil().


{code}
public Date ciel(Date d, int field) {
        d = DateUtils.add(d, field, +1);
        d = DateUtils.truncate(d, field);
        return d;
}
{code}

Quick visual test:

 Date d;
        d = new Date();
        System.out.println("now=" + d);
        System.out.println("ciel=" + ciel(d, Calendar.YEAR));
        System.out.println("ciel=" + ciel(d, Calendar.MONTH));
        System.out.println("ciel=" + ciel(d, Calendar.DAY_OF_MONTH));
        System.out.println("ciel=" + ciel(d, Calendar.HOUR));
        System.out.println("ciel=" + ciel(d, Calendar.MINUTE));



      was (Author: prophecy):
    Not a patch, but this should work as the equivalent of the Math.ceil().

 public Date ciel(Date d, int field) {
        d = DateUtils.add(d, field, +1);
        d = DateUtils.truncate(d, field);
        return d;
    }

Quick visual test:

 Date d;
        d = new Date();
        System.out.println("now=" + d);
        System.out.println("ciel=" + ciel(d, Calendar.YEAR));
        System.out.println("ciel=" + ciel(d, Calendar.MONTH));
        System.out.println("ciel=" + ciel(d, Calendar.DAY_OF_MONTH));
        System.out.println("ciel=" + ciel(d, Calendar.HOUR));
        System.out.println("ciel=" + ciel(d, Calendar.MINUTE));


  
> Add DateUtils.ceiling() method
> ------------------------------
>
>                 Key: LANG-434
>                 URL: https://issues.apache.org/jira/browse/LANG-434
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 2.4
>            Reporter: Travis Reeder
>             Fix For: 3.0
>
>
> It would be nice to have a DateUtils.ceiling methods which would be the opposite of truncate (which is equivalent to floor)
> DateUtils.ceiling(Date d, int field);
> For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with HOUR, it would return 28 Mar 2002 13:59:59.999. If this was passed with MONTH, it would return 31 Mar 2002 23:59:59.999.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.