如何使用 String.format(Locale, ...)?

发布于 2025-01-20 22:13:14 字数 599 浏览 3 评论 0原文

我是 Android 开发新手,我正在尝试这段代码:

String hms = String.format("%02d:%02d:%02d",
               TimeUnit.MILLISECONDS.toHours(milliSeconds),
               TimeUnit.MILLISECONDS.toMinutes(milliSeconds) - 
               TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(milliSeconds)),
               TimeUnit.MILLISECONDS.toSeconds(milliSeconds) -
               TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliSeconds)));

我收到消息:

隐式使用默认区域设置是错误的常见来源:

使用 String.format(Locale, ...) 代替

我不知道如何修改代码以实现建议。

I am new in Android development and I'm trying this code:

String hms = String.format("%02d:%02d:%02d",
               TimeUnit.MILLISECONDS.toHours(milliSeconds),
               TimeUnit.MILLISECONDS.toMinutes(milliSeconds) - 
               TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(milliSeconds)),
               TimeUnit.MILLISECONDS.toSeconds(milliSeconds) -
               TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliSeconds)));

I get the message :

Implicitly using the default locale is a common source of bugs:

Use String.format(Locale, ...) instead

I have no idea how to modify the code in order to implement the recommendation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛清羽墨安 2025-01-27 22:13:14

您很可能无需做任何事情。这是一个警告,告诉您您没有为String.Format指定语言环境,因此它使用默认场所。在某些情况下,这可能会导致错误,但这不太可能在您的情况下。如果您想小心,可以在明确的地方传递,也可以忽略警告。这样的格式数字没有任何类型的货币是相当安全的。

(您会看到的错误是,如果您的设备处于特定的格式规则。

Most likely you don't need to do anything. That's a warning telling you that you didn't specify a locale for String.format, so it's using the default locale. That can cause a bug in some circumstances, but it's unlikely to in yours. If you want to be careful you can pass in an explicit locale, or you can just ignore the warning. Formatting numbers like this without any type of currency is fairly safe.

(The bugs you'll see are if the locale your device is in has specific formatting rules for things. The big one I know of that's hit me is that Turkish has a letter i who's capital symbol is different than the english letter I.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文