SimpleDateFormat(String template, Locale locale) with for example Locale.US for ASCII dates

后端 未结 3 1409
遥遥无期
遥遥无期 2021-01-31 07:48

Issue: Using SimpleDateFormat directly without an explicit locale Id: SimpleDateFormat

SimpleDateFormat format = new SimpleDateFormat(         


        
3条回答
  •  后悔当初
    2021-01-31 08:09

    It's a dumb lint warning. If you look at the SimpleDateFormat constructor source code it gets the default locale.

    public SimpleDateFormat(String pattern) {
        this(pattern, Locale.getDefault());
    }
    

    So adding it in your code is redundant and unnecessarily verbose. Locale.getDefault() is almost always what you want since that is what the user's device is set to. If for some reason you need it to always return, for example, "Monday" no matter what the user's language is set to than you can specify Locale.US but that seems like a rare situation.

    The best thing to do is disable the dumb inspection.

提交回复
热议问题