English
Date
About 360 wordsAbout 1 min
2026-01-09
Date - Represents a date in YYYY-MM-DD format, enclosed in double quotes when used.
Define Date: Date date = "<YYYY-MM-DD>"
Example:
Date date = "2019-01-01"Date type methods:
Date.now(): Gets the current date (year, month, day) Example:
Date d = Date.now()date.withYear(<Integer year>): Sets the year of the date and returns a new date Return type: Date
Example:
date.withYear(2018) //Returns: 2018-01-01date.withMonth(<Integer month>): Sets the month of the date and returns a new date Return type: Date
Example:
date.withMonth(12) //Returns: 2019-12-01date.withDay(<Integer day>): Sets the day of the date and returns a new date Return type: Date
Example:
date.withDay(30) //Returns: 2019-01-30date.toTimestamp(): Converts date to timestamp Return type: Long
Example:
date.toTimestamp() //Returns: 1567958400000date.year: Gets the year from the date Example:
date.year //Returns: 2019date.month: Gets the month from the date Example:
date.month //Returns: 1date.day: Gets the day from the date Example:
date.day //Returns: 1date.dayOfWeek: Gets the day of week (1=Monday) Example:
date.dayOfWeek //Returns: 1 (Monday)date.weekOfYear: Gets the week number of the year Example:
date.weekOfYear //Returns: 1 (First week of the year)date.weekOfMonth: Gets the week number of the month Example:
date.weekOfMonth //Returns: 1 (First week of the month)date.dayOfYear: Gets the day number of the year Example:
date.dayOfYear //Returns: 1 (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) //Returns: 2date.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) //Returns: 2date.toStartOfMonth(): Returns the first day of the month Example:
Date date = "2020-01-20"
Date dateRetrun = date.toStartOfMonth() //Returns: 2020-01-01date.toStartOfWeek(): Returns the first day of the week (Monday) Example:
Date date = "2020-01-01"
Date dateRetrun = date.toStartOfWeek() //Returns: 2020-12-30Date.of(Long timestamp): Converts timestamp to date Example:
Long timestamp = 1618972431890
Date d = Date.of(timestamp)Date.of(<String a>): Converts string to date Example:
String a = "2020-01-01"
Date date = Date.of(a)
log.info("date:"+date)