Use "Instant.truncatedTo" instead of DateUtils.truncate

DateUtils.truncate method of DateUtils class is significantly slower in compare to trunc method Instant class. Java 8 Instant class has been introduced to gain the better performance.

My use case: 

In my case I had the following date: Thu Jun 28 08:20:06 US 2018. I wanted to only get the date by removing the hour:minute:second part from date object. The date should be Thu Jun 28 00:00:00 US 2018.

My noncompliant code:

As I have used DateUtils class a lot so I decided to do it using DateUtils.truncate method. It worked and things were all good. When my code on reviewed by java analyzer it raised this issue: Use "Instant.truncatedTo" instead of DateUtils.truncate

The Solution:

I didn't make it work with Java 8 Instant class but I used Calendar class to solve the issue. Following is the method that I used to remove the hour:minute:second from date object. Instesting thing is the DateUtils class also uses Calendar class to remove the hour minute and second from date object. The analyzer was okay with the following function:

    private Date trunc(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

Comments