[ https://issues.apache.org/jira/browse/GROOVY-7964?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15573436#comment-15573436 ]
Paul King commented on GROOVY-7964: ----------------------------------- Well, it is either a bug or different assumptions about time zones. > 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 > Labels: timecategory > Attachments: DatesDiff.groovy > > > TimeCategory incorrectly counts days difference between two dates. > Exhibit (Groovy code): > {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" > } > // Joda 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 "Joda Time: Dates between $date1 and $date2 : $days" > {code} > Current Output: > {noformat} > $ 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 > Joda Time: Dates between 2016-03-22 and 2016-03-29 : 7 > {noformat} > Expected Output: > {noformat} > $ 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 > Joda Time: Dates between 2016-03-22 and 2016-03-29 : 7 > {noformat} > Resume: notice the difference in "Groovy TimeCategory/Date" line. -- This message was sent by Atlassian JIRA (v6.3.4#6332)