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

问题描述:

从1月到现在,我有一个域工作,其ID,日期,列出日期. 我通过代码获取当前时间:

I got a domain work with id, day, list day from January to now. I get the current time by code:

def current = new Date()

因此,我想获得最近两周的清单日,包括本周在内,然后我使用了以下代码,但它不起作用.

So, I'd like to get list day from last 2 weeks, included this week, then I used the following code but it doesn't work.

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

请帮助我解决问题.

我不能百分百确定,但您应该可以使用Range:

Not 100% sure I understand, but you should be able to use a Range:

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周内进行操作,则可以执行以下操作:

If you mean you want the entire 2 weeks before the current week AND the current week, you could do:

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