Skip to content

Groovy 42 – Extract Time Information from a Date-Time Field

To extract the information from a Date-Time  we can use Calendar class in combination with the built in time methods.

Lets say we have a custom field of type date-time called DateTime_c

We want to extract from the value of this field the following:

– year

– month

– day

– hours

– minutes

– seconds

– milliseconds

For  year, month, day we use the built in functions as follows:

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();  

Now we have access to all the methods of this class.

So we use MINUTE , HOUR_OF_DAY ,  SECOND,  MILLISECOND as follows:

int integerHour = calendar.get(Calendar.HOUR_OF_DAY);  
int integerMinute = calendar.get(Calendar.MINUTE);  
int integerSeconds = calendar.get(Calendar.SECOND);  
int integerMiliseconds  = calendar.get(Calendar.MILLISECOND);  





Leave a Reply

Your email address will not be published. Required fields are marked *