Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonar - Make DATE_FORMAT as instance variable

I have a rest web service, and below is how i have declared DateFormat as this is the date format i am going to use application wide.

When i did code analysis using SonarLint eclipse plug-in, i got major warning saying "Make DATE_FORMAT as instance variable."

public class Constants {

    private Constants() {

    }

    public static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS");

}

Can anyone tell me what issue i might face if i use it this way in my rest API ?
If i use it as instance variable i will end up declaring it in multiple classes ?

like image 533
Jigar Naik Avatar asked Mar 04 '19 07:03

Jigar Naik


1 Answers

Static variable are mostly used for Constants.
Here you have declared static and assigning it instance of SimpleDateFormat.
Either make DATE_TIME_FORMAT non-static or assign a constant to this variable.

Better change it to instance variable and use a Sting to do that.
e.g public final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss:SSS";

like image 187
TheSprinter Avatar answered Nov 15 '22 00:11

TheSprinter