[ https://issues.apache.org/jira/browse/GROOVY-7964?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Los Pejos updated GROOVY-7964: ------------------------------ Attachment: DatesDiff.groovy > TimeCategory incorrectly counts days difference between two dates > ----------------------------------------------------------------- > > Key: GROOVY-7964 > URL: https://issues.apache.org/jira/browse/GROOVY-7964 > Project: Groovy > Issue Type: Bug > Components: groovy-runtime > Affects Versions: 2.4.7 > Environment: Windows 10 x86_64, Oracle Java 1.8.0.102 x86_64 > Reporter: Los Pejos > Attachments: DatesDiff.groovy > > > TimeCategory incorrectly counts days difference between two dates. > Exhibit (Groovy code): > #!/usr/bin/env groovy > @Grab(group='joda-time', module='joda-time', version='2.9.4+') > import java.io.BufferedReader > import java.io.IOException > import java.io.InputStreamReader > import java.time.LocalDate > import java.time.format.DateTimeFormatter > import java.time.temporal.ChronoUnit > import groovy.time.TimeCategory > String date1 = '2016-03-22' > String date2 = '2016-03-29' > String fmt = 'yyyy-MM-dd' > // Java 8 ChronoUnit: > DateTimeFormatter formatter = DateTimeFormatter.ofPattern(fmt) > //LocalDate firstDate = LocalDate.parse(date1, formatter) > //LocalDate secondDate = LocalDate.parse(date2, formatter) > long days = ChronoUnit.DAYS.between( > LocalDate.parse(date1, formatter), > LocalDate.parse(date2, formatter) > ) > println "Java 8 ChronoUnit: Dates between $date1 and $date2 : $days" > // Groovy TimeCategory/Date: > use (TimeCategory){ > def d = (Date.parse(fmt, date2) - Date.parse(fmt, date1)).days > println "Groovy TimeCategory/Date: Dates between $date1 and $date2 : $d" > } > // Jode Time: > // LocalDate fromDateFields(Date date) > // Date.parse('yyyy-MM-dd', dlf.@dldpf.toString()) > days = org.joda.time.Days.daysBetween( > org.joda.time.LocalDate.fromDateFields(Date.parse(fmt,date1)), > org.joda.time.LocalDate.fromDateFields(Date.parse(fmt,date2)) > ).getDays() > println "Jode Time: Dates between $date1 and $date2 : $days" > Current Output: > $ groovy DatesDiff.groovy > Java 8 ChronoUnit: Dates between 2016-03-22 and 2016-03-29 : 7 > Groovy TimeCategory/Date: Dates between 2016-03-22 and 2016-03-29 : 6 > Jode Time: Dates between 2016-03-22 and 2016-03-29 : 7 > Expected Output: > $ groovy DatesDiff.groovy > Java 8 ChronoUnit: Dates between 2016-03-22 and 2016-03-29 : 7 > Groovy TimeCategory/Date: Dates between 2016-03-22 and 2016-03-29 : 7 > Jode Time: Dates between 2016-03-22 and 2016-03-29 : 7 > Resume: notice the difference in "Groovy TimeCategory/Date" line. -- This message was sent by Atlassian JIRA (v6.3.4#6332)