Groovy : Find the date of the previous Monday -
In the following code, I want to search on the last Monday date. For this, I have two variables:
- startday = today - 7 days
- stop days = today - 1 day (tomorrow)
And I have a function that lists all the dates between "starti" and "stoppage", and find these dates that match on Monday.
This works fine when I have two dates:
- startday = 2014-07-20
- stop DAY = 2014-07- 29
But, when the decade of the two changes, code ends with an error:
- startday = 2014-07-29
- Stops = 2014-07-30
Error:
java.lang.IllegalArgumentException: Incompatible string for category: string # next ( ) Will not reach the expected value
Code:
def search command = {start, stop> (Start .. Stop) .FindAll {Date.parse ("yyyy-MM-dd", "$ {it}") Format ("U") == "1"}} def startDay = new java.text format (new date () - 7) def stopDay = new java.text.SimpleDateFormat ("yyyy-MM-dd") format (new Date () - 1) def dateOfTheDay = Search (Start, Stop); Def dateOfTheDayWithoutSquare = dateOfTheDay.join (",") Date of Return DayWithoutSquare of
You will get the last Monday starting today
def cal = Calendar.instance while (cal.get (Calendar. DAY_OF_WEEK)! = Calendar. MONDAY) {cal.add (Calendar. DAY_OF_WEEK, -1 )} Date last Monday = print the date in cal.time // yyyy-MM-dd format println lastMonday.format
If you search last Monday Want to replace the first line with some other dates like this:
def cal = Calendar.insta Nce date Some other data = // Get a date from a call. Time = some other date
Comments
Post a Comment