Groovy,获取当前周和过去2周的列表

时间:2014-04-22 09:54:38

标签: groovy

我从1月到现在有一​​个id,day,list day的域名工作。 我通过代码获得当前时间:

def current = new Date()

所以,我想在本周收到过去2周的列表日,然后我使用了以下代码,但它没有用。

def getWeek = current.Time - 13 (13 is 2 week + today)

请帮我解决。

1 个答案:

答案 0 :(得分:2)

不是100%肯定我明白,但你应该能够使用范围:

def current = new Date().clearTime()

def listOfDays = (current - 13)..current

listOfDays.each { println it }

打印:

Wed Apr 09 00:00:00 BST 2014
Thu Apr 10 00:00:00 BST 2014
Fri Apr 11 00:00:00 BST 2014
Sat Apr 12 00:00:00 BST 2014
Sun Apr 13 00:00:00 BST 2014
Mon Apr 14 00:00:00 BST 2014
Tue Apr 15 00:00:00 BST 2014
Wed Apr 16 00:00:00 BST 2014
Thu Apr 17 00:00:00 BST 2014
Fri Apr 18 00:00:00 BST 2014
Sat Apr 19 00:00:00 BST 2014
Sun Apr 20 00:00:00 BST 2014
Mon Apr 21 00:00:00 BST 2014
Tue Apr 22 00:00:00 BST 2014

如果您的意思是想要在本周和本周之前的整个2周,您可以这样做:

def current = new Date().clearTime()

int currentDay = Calendar.instance.with {
    time = current
    get( Calendar.DAY_OF_WEEK )
}

def listOfDays = (current - 13 - currentDay)..(current + 7 - currentDay)

listOfDays.each {
    println it
}

打印哪些:

Sun Apr 06 00:00:00 BST 2014
Mon Apr 07 00:00:00 BST 2014
Tue Apr 08 00:00:00 BST 2014
Wed Apr 09 00:00:00 BST 2014
Thu Apr 10 00:00:00 BST 2014
Fri Apr 11 00:00:00 BST 2014
Sat Apr 12 00:00:00 BST 2014
Sun Apr 13 00:00:00 BST 2014
Mon Apr 14 00:00:00 BST 2014
Tue Apr 15 00:00:00 BST 2014
Wed Apr 16 00:00:00 BST 2014
Thu Apr 17 00:00:00 BST 2014
Fri Apr 18 00:00:00 BST 2014
Sat Apr 19 00:00:00 BST 2014
Sun Apr 20 00:00:00 BST 2014
Mon Apr 21 00:00:00 BST 2014
Tue Apr 22 00:00:00 BST 2014
Wed Apr 23 00:00:00 BST 2014
Thu Apr 24 00:00:00 BST 2014
Fri Apr 25 00:00:00 BST 2014
Sat Apr 26 00:00:00 BST 2014