English
DateTime
About 358 wordsAbout 1 min
2026-01-09
Date - A concrete date type formatted as YYYY-MM-DD hh:mm, enclosed in quotes when used.
Define Date: DateTime dateTime = "<YYYY-MM-DD hh:mm>"
Example:
DateTime dateTime = "2019-01-01 12:00"DateTime type methods:
- DateTime.now(): Gets the current year, month, day, hour and minute Example:
DateTime d = DateTime.now()- dateTime.withYear(
<Integer year>): Sets the year of the date and returns a new date Return type: DateTime
Example:
dateTime.withYear(2018) //Returns: 2018-01-01 12:00- dateTime.withMonth(
<Integer month>): Sets the month of the date and returns a new date Return type: DateTime
Example:
dateTime.withMonth(12) //Returns: 2019-12-01 12:00- dateTime.withDay(
<Integer day>): Sets the day of the date and returns a new date Return type: DateTime
Example:
dateTime.withDay(30) //Returns: 2019-01-30 12:00- dateTime.withHour(
<Integer hour>): Sets the hour of the date and returns a new date Return type: DateTime
Example:
dateTime.withHour(13) //Returns: 2019-01-01 13:00- dateTime.withMinute(
<Integer day>): Sets the minute of the date and returns a new date Return type: DateTime
Example:
dateTime.withMinute(30) //Returns: 2019-01-01 12:30- dateTime.toTimestamp(): Converts date to timestamp Return type: Long
Example:
dateTime.toTimestamp() //Returns: 1568001600000- dateTime.year: Gets the year from the date Example:
dateTime.year //Returns: 2019- dateTime.month: Gets the month from the date Example:
dateTime.month //Returns: 1- dateTime.day: Gets the day from the date Example:
dateTime.day //Returns: 1- dateTime.hour: Gets the hour from the date Example:
dateTime.hour //Returns: 12- dateTime.minute: Gets the minute from the date Example:
dateTime.minute //Returns: 0- dateTime.dayOfWeek: Gets the day of week for current date Example:
dateTime.dayOfWeek //Returns: 1(Monday)- dateTime.weekOfYear: Gets the week number of year for current date Example:
dateTime.weekOfYear //Returns: 1(First week of year)- dateTime.weekOfMonth: Gets the week number of month for current date Example:
dateTime.weekOfMonth //Returns: 1(First week of month)- dateTime.toDate: Converts DateTime to Date Example:
DateTime dateTime = "2019-01-01 12:00"
Date date = dateTime.toDate()
log.info(date)- DateTime.of(Long timestamp): Converts timestamp to DateTime
Long timestamp = 1618972431890
DateTime d1 = DateTime.of(timestamp)- DateTime.of(
<String a>): Converts string to date Example:
String b = "2020-01-01 00:00"
DateTime dateTime = DateTime.of(b)
log.info("dateTime"+ dateTime)