Some useful groovy tips :
1. List Size : Often we used to find list is empty or not. For this we do something like :
List numberList = [1,2,3] // list in this case
if(numberList.size() == 0){
...
}
else{
...
}
Rather than doing this, we can simply use groovy advancement like :
if(!numberList){ // checks is list empty?
...
}
else{
...
}
2. Finding current day,month and year from the date :
As Date methods are deprecated to find out current day, month and year from date object and other information.
We can use Calender Object for this like (current date is 29/08/2014):
Calendar now = Calendar.getInstance() // Gets the current date and time.
def year= now.get(Calendar.YEAR); // year = 2014
def month= now.get(Calendar.MONTH)+1 ; // month = (8+1) = 9
// Reason for incrementing by 1 is that first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last
depends on the number of months in a year.
So if your current date is 29/08/2014 then it will return month as 8.
def day = now.get(Calendar.DAY_OF_MONTH) // day = 29
You can also find more information about zone, week of year,week of month and hour of day etc. from Calender object
Thanks
Shiv Kumar
More From Oodles
Ready to innovate? Let's get in touch
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Shiv Kumar
Shiv is an experienced Java Developer with a strong background in multiple technologies. He specializes in defining system architectures to ensure reliable and resilient solutions. He possesses a comprehensive understanding of the latest technologies and has hands-on experience in Core Java, Spring Boot, Hibernate, Apache Kafka messaging queue, Redis, as well as relational databases like MySQL and PostgreSQL, and non-relational databases like MongoDB. He excels in API implementations, Microservices, Web Services development, testing, and deployments. Shiv actively contributes to code enhancements and consistently delivers valuable contributions to various client projects, including Fabtrack, Pando, Pandojo, Digikam, WhatsApp Integration, Croniz, Punchin Application, Script TV, Bhaasha, and more. He demonstrates strong analytical skills and a creative mindset. In addition, he has a passion for reading books and exploring new technologies and innovations.