Skip to content

Instantly share code, notes, and snippets.

@nallegrotti
Created March 25, 2015 19:29
Show Gist options
  • Save nallegrotti/c09204e0d65a5dc207f3 to your computer and use it in GitHub Desktop.
Save nallegrotti/c09204e0d65a5dc207f3 to your computer and use it in GitHub Desktop.
Calculo de Días hábiles entre 2 fechas (no incluye feriados)
import groovy.time.TimeCategory
/// Magia
def weekendDays(from, to){
use(groovy.time.TimeCategory){
def d = (to - from).days
def w = d/7 as Integer
def toWD = to[Calendar.DAY_OF_WEEK]
def fromWD = from[Calendar.DAY_OF_WEEK]
java.lang.Math.min(w * 2 + (fromWD > toWD ? 2 : (toWD == 7 ? 1:0) + (fromWD == 1 ? 1:0)), d)
}
}
/// Test
use(TimeCategory){
Date from = new Date('2015/05/15')
Date to = new Date('2015/05/23')
for ( ; from < to ; from = from + 1.day) {
def elapsedTime = to - from
println "$from -> $to"
println elapsedTime.days
println "-"*50
println from[Calendar.DAY_OF_WEEK] + "."*elapsedTime.days + to[Calendar.DAY_OF_WEEK]
def we = weekendDays(from, to)
println "Weekend days $we -> Bussines days: ${elapsedTime.days - we}"
println "."*50
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment