Date - date type, the format is YYYY-MM-DD, when used, it is encapsulated with ""
Define Date: Date date = "<YYYY-MM-DD>"
example:
Date date = "2019-01-01"
Date type method:
-
Date.now(): Get the year, month, and day of the current time
example:
Date d = Date.now()
- date.withYear(<Integer year>): Set the year of the date and return the new date
Return value type: Date
example:
date.withYear(2018) //Return: 2018-01-01
- date.withMonth(<Integer month>): set the month of the date and return the new date
Return value type: Date
example:
date.withMonth(12) //Return: 2019-12-01
- date.withDay(<Integer day>): set the day of the date and return the new date
Return value type: Date
example:
date.withDay(30) //Return: 2019-01-30
- date.toTimestamp(): date to timestamp
Return value type: Long
example:
date.toTimestamp() //return: 1567958400000
-
date.year: get the year in the date
example:
date.year //return: 2019
-
date.month: get the month in the date
example:
date.month // returns: 1
-
date.day: Get the day in the date
example:
date.day //return: 1
-
date.dayOfWeek: The current date is the day of the week
example:
date.dayOfWeek //Return: 1 (Monday)
-
date.weekOfYear: The current date is the week of the year
example:
date.weekOfYear //Return: 1 (the first week of the year)
-
date.weekOfMonth: The current date is the week of the month
example:
date.weekOfMonth //return: 1 (the first week of the month)
-
date.dayOfYear: The current date is the day of the year
example:
date.dayOfYear //Return: 1 (the first day of the year)
-
date.daysBetween(<Date date>): returns the number of days between two dates
example:
Date date1 = "2020-01-02"
Date date2 = "2020-01-03"
date1.daysBetween(date2) //return: 2
-
date.monthsBetween(<Date date>): returns the number of months between two dates
example:
Date date1 = "2020-01-01"
Date date2 = "2020-03-03"
date1.monthsBetween(date2) //return: 2
-
date.toStartOfMonth(): returns the start date of this month
example:
Date date = "2020-01-20"
Date dateRetrun = date.toStartOfMonth() //Return: 2020-01-01
-
date.toStartOfWeek(): returns the start date of this week
example:
Date date = "2020-01-01"
Date dateRetrun = date.toStartOfWeek() //Return: 2020-12-30
-
Date.of(Long timestamp): convert timestamp to date
example:
Long timestamp = 1618972431890
Date d = Date.of(timestamp)
-
Date.of(<String a>): string to date
example:
String a = "2020-01-01"
Date date = Date.of(a)
log.info("date:"+date)