Groovy 42 – Extract Time Information from a Date-Time Field
Date-Time Extraction in Groovy
To extract information from a Date-Time field, we can use the Calendar
class in combination with built-in time methods.
Assume we have a custom field of type date-time called DateTime_c
. We want to extract the following:
- Year
- Month
- Day
- Hours
- Minutes
- Seconds
- Milliseconds
For year, month, and day, we use the built-in functions:
int integerYear = year(DateTime_c); int integerMonth = month(DateTime_c); int integerDay = day(DateTime_c);
For hours, minutes, seconds, and milliseconds, we first instantiate the Calendar
class:
Calendar calendar = Calendar.getInstance(); calendar.setTime(DateTime_c); int integerHour = calendar.get(Calendar.HOUR_OF_DAY); int integerMinute = calendar.get(Calendar.MINUTE); int integerSeconds = calendar.get(Calendar.SECOND); int integerMilliseconds = calendar.get(Calendar.MILLISECOND);