Date - date type (specific), the format is YYYY-MM-DD hh:mm, encapsulated with "" when used
Define Date: DateTime dateTime = "<YYYY-MM-DD hh:mm>"
example:
> DateTime dateTime = "2019-01-01 12:00"
Methods of DateTime type:
-
DateTime.now(): Get the year, month, day, hour and minute of the current time
example:
> DateTime d = DateTime.now()
- dateTime.withYear(<Integer year>): Set the year of the date and return the new date
Return value type: DateTime
example:
> dateTime.withYear(2018) //Return: 2018-01-01 12:00
- dateTime.withMonth(<Integer month>): set the month of the date and return the new date
Return value type: DateTime
example:
> dateTime.withMonth(12) //Return: 2019-12-01 12:00
- dateTime.withDay(<Integer day>): set the day of the date and return the new date
Return value type: DateTime
example:
> dateTime.withDay(30) //Return: 2019-01-30 12:00
- dateTime.withHour(<Integer hour>): set the hour of the date and return the new date
Return value type: DateTime
example:
> dateTime.withHour(13) //Return: 2019-01-01 13:00
- dateTime.withMinute(<Integer day>): set the minute of the date and return the new date
Return value type: DateTime
example:
> dateTime.withMinute(30) //Return: 2019-01-01 12:30
- dateTime.toTimestamp(): date to timestamp
Return value type: Long
example:
> dateTime.toTimestamp() //return: 1568001600000
-
dateTime.year: get the year in the date
example:
> dateTime.year //return: 2019
-
dateTime.month: get the month in the date
example:
> dateTime.month // returns: 1
-
dateTime.day: Get the day in the date
example:
> dateTime.day // returns: 1
-
dateTime.hour: get the hour in the date
example:
> dateTime.hour //return: 12
-
dateTime.minute: Get the minute in the date
example:
> dateTime.minute //return: 0
-
dateTime.dayOfWeek: The current date is the day of the week
example:
> dateTime.dayOfWeek //Return: 1 (Monday)
-
dateTime.weekOfYear: The current date is the week of the year
example:
> dateTime.weekOfYear // Returns: 1 (the first week of the year)
-
dateTime.weekOfMonth: The current date is the week of the month
example:
> dateTime.weekOfMonth // Returns: 1 (the first week of the month)
-
dateTime.toDate: date time to date
example:
> DateTime dateTime = "2019-01-01 12:00"
>
> Date date = dateTime.toDate()
>
> log. info(date)
- DateTime.of(Long timestamp): convert timestamp to dateTime
Long timestamp = 1618972431890
DateTime d1 = DateTime.of(timestamp)
- DateTime.of(<String a>): string to date
example:
String b = "2020-01-01 00:00"
DateTime dateTime = DateTime.of(b)
log.info("dateTime"+ dateTime)