[ 
https://issues.apache.org/jira/browse/LANG-1629?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17252173#comment-17252173
 ] 

LeeJuHyun commented on LANG-1629:
---------------------------------

Hi [~kinow],

Thank you very much for your quick reply. 

I was so impressed your quick reply

I said wrong thing, so sorry..

 

I used *formatPeriod* method that has a *3 parameters*(250 line in 
DurationFormatUtils class)
{code:java}
    public static String formatPeriod(long startMillis, long endMillis, String 
format) {
        return formatPeriod(startMillis, endMillis, format, true, 
TimeZone.getDefault());
    } {code}
 

and my code snippet is like that ..
{code:java}
String startDate = "20201219";
String startTime = "203000";
String endDate = "00000000";
String endTime = "000000";
long stime = MyClass.toBaseDate(startDate, startTime).getTime(); // 
1608377400000
long etime = MyClass.toBaseDate(endDate, endTime).getTime();     // 
-62170189200000

DurationFormatUtils.formatPeriod(stime, etime, "HH:mm:ss");

{code}
 
{code:java}
    /* This is my Project code */
    public static Date toBaseDate(String yyyymmdd, String hhmmss) {
        return toBasicDate(yyyymmdd, hhmmss, "yyyyMMdd HHmmss");
    }
    
    private static Date toBaseDate(String yyyymmdd, String hhmmss, String 
format) {
        Date date = new Date();
        if(!StringUtil.isEmpty(yyyymmdd)) {
            DateFormat df = new SimpleDateFormat(format);
            try {
                date = df.parse(yyyymmdd + " " + hhmmss);
            } catch(ParseException e) {
                e.printStackTrace();
            }
        }
        return date;    
} {code}
== result ==

stime = 1608377400000

etime = -62170189200000

 

if stime is 1608377400000, then an error(infinite-loops) occurs regardless of 
whether etime is 1 or -1.

 

I'm using *DurationFormatUtils.formatPeriod* in my project.

*stime* and *etime* can be obtained from *toBaseDate* method.

 

usually startDate, startTime, endDate, endTime is formatting data.

(*Date = yyyyMMdd, *Time = HHmmss , not zero)

 

but last week, the formatting data is wrong.

(endDate = "00000000", endTime = "000000")

so, *DurationFormatUtils.formatPeriod* method has become an infinite-loops.

 

I modified my code to solve this problem, but ultimately, the infinite-loops 
occurring in *DurationFormatUtils.formatPeriod* could not be solved.

 

I'm trying to solve this problem, but as a junior developer, the code is 
difficult for me.

 

Thank you very much :)

 

> DurationFormatUtils.formatPeriod is infinite repetition when parameter is 
> negative number
> -----------------------------------------------------------------------------------------
>
>                 Key: LANG-1629
>                 URL: https://issues.apache.org/jira/browse/LANG-1629
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.time.*
>    Affects Versions: 3.11
>            Reporter: LeeJuHyun
>            Priority: Major
>             Fix For: Discussion
>
>
> if parameter number(*startMillis*, *endMillis*) is negative number then below 
> while loop is infinite repetition. (278 line ~)
> {code:java}
> public static String formatPeriod(final long startMillis, final long 
> endMillis, final String format, final boolean padWithZeros,
>         final TimeZone timezone) {
>     Validate.isTrue(startMillis <= endMillis, "startMillis must not be 
> greater than endMillis");
>     // Used to optimise for differences under 28 days and
>     // called formatDuration(millis, format); however this did not work
>     // over leap years.
>     // TODO: Compare performance to see if anything was lost by
>     // losing this optimisation.
>     final Token[] tokens = lexx(format); 
>     // ...
>     while (start.get(Calendar.YEAR) != target) {
>     days += start.getActualMaximum(Calendar.DAY_OF_YEAR) - 
> start.get(Calendar.DAY_OF_YEAR);
>     // Not sure I grok why this is needed, but the brutal tests show it is
>     if (start instanceof GregorianCalendar &&
>             start.get(Calendar.MONTH) == Calendar.FEBRUARY &&
>             start.get(Calendar.DAY_OF_MONTH) == 29) {
>         days += 1;
>     }
>     start.add(Calendar.YEAR, 1);
>     days += start.get(Calendar.DAY_OF_YEAR);
> }{code}
>  
> How about putting in a validation to determine whether it's negative or 
> positive?
>  
> thank you :)
>  
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to