Created
March 25, 2015 19:29
-
-
Save nallegrotti/c09204e0d65a5dc207f3 to your computer and use it in GitHub Desktop.
Calculo de Días hábiles entre 2 fechas (no incluye feriados)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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