SimpleDateFormat warning To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(),

后端 未结 3 1904
再見小時候
再見小時候 2020-12-25 10:30

Do i need to be worried about this warning? What if I ignore the warning?
What does this warning mean:
To get local formatting use getDateInstance(),

3条回答
  •  清酒与你
    2020-12-25 11:29

    You are currently using the SimpleDateFormat(String) constructor. This implies the default locale and as the Locale documentation tells you, be wary of the default locale as unexpected output can be produced on various systems.

    You should instead use the SimpleDateFormat(String, Locale) constructor. It is going to take in an additional parameter - the locale you want to use. If you want to make sure the output is machine-readable in a consistent way (always looks the same, regardless of the actual locale of the user), you can pick Locale.US. If you do not care about machine redability, you can explicitly set it to use Locale.getDefault().

    Using those on your example code would look something like this:

    // for US
    SimpleDateFormat sdfDate = new SimpleDateFormat("d MMM yyyy", Locale.US);
    
    // or for default
    SimpleDateFormat sdfDate = new SimpleDateFormat("d MMM yyyy",
            Locale.getDefault());
    

提交回复
热议问题