2014-04-22 61 views
0

從1月到現在,我得到了一個帶有id,day,list日期的域名。 我通過代碼獲取當前時間:Groovy,獲取本週和最近2周的列表

def current = new Date() 

所以,我想獲得清單一天從過去的2周,包括這個星期,然後我用下面的代碼,但它不工作。

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

請幫我解決它。

回答

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 
+1

我做了,掛上,我還有一個問題,在當前日期,我們該如何計算當前周的日期?例如:周從週日開始到週六。然後,今天是星期一,當星期幾= 2時,如果星期二,星期幾是3.請幫助,謝謝 –

+0

您的意思是:'int day = Calendar.instance.with {time = current;獲得(Calendar.DAY_OF_WEEK)}'? –

+0

這裏是我的想法,我會顯示最近2周和當前周的信息,然後到星期一,我使用當前日期 - 16(16意味着最後2周)來查找總日期,然後下一步,那是如果當前是星期四,我只能使用星期一,我該如何計算?這是我的觀點。 –