简体   繁体   中英

SimpleDateFormat and a strange warning

This is the part of the code with this "strange" warning:

public void onTick(long millisUntilFinished) {
    tv.setText("" + new SimpleDateFormat("mm:ss").format(new Date( millisUntilFinished)));
}

The warning is that:

To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(), or use new SimpleDateFormat(String template, Locale locale) with for example Locale.US for ASCII dates.

It's strange for me because I use SimpleDateFormat so I don't understand the reason why the advice for avoiding the warning is exactly to do what it was ever done. Moreover, I use the same thing in other activity without any warning so I compare completely the two codes and there is no difference that could explain that (the two forementioned activities only differ by some constant names.)

Besides, I want to know if it could imply further problems because my program works fine by the way.

To remove the warning just add Locale.getDefault() as the second argument while instantiating the date format object. Eg.

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                java.util.Locale.getDefault());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM