[ https://issues.apache.org/jira/browse/GROOVY-7964?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15574595#comment-15574595 ]
Paul King commented on GROOVY-7964: ----------------------------------- Here is a script illustrating the equivalent Groovy and Jodatime functionality: {code} @Grab('joda-time:joda-time:2.9.4') import groovy.time.TimeCategory import org.joda.time.* String date1 = '2016-03-22', date2 = '2016-03-29', fmt = 'yyyy-MM-dd' TimeZone.setDefault TimeZone.getTimeZone('Europe/Berlin') use (TimeCategory) { def diff = (Date.parse(fmt, date2) - Date.parse(fmt, date1)) assert diff.toString() == '6 days, 23 hours' assert diff.days * 24 + diff.hours == 167 } def dtz = DateTimeZone.forTimeZone(TimeZone.default) assert Hours.hoursBetween(new DateTime(date1, dtz), new DateTime(date2, dtz)).hours == 167 assert Hours.hoursBetween(new DateTime(date1, dtz), new DateTime(date2, dtz)).toStandardDays().days == 6 {code} It possibly is a little hidden but buried within the groovydocs it indicates that daylight savings is taken into account when adding/subtracting days. Even leap seconds are taken into account! If you want something that is daylight savings free, you could try: {code} def gmt = TimeZone.getTimeZone('GMT') use (TimeCategory) { def diff = (Date.parse(fmt, date2, gmt) - Date.parse(fmt, date1, gmt)) assert diff.toString() == '7 days' } {code} Perhaps the documentation needs improvement. Pull requests welcome! :-) > 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)