首页 > 解决方案 > meridiem 由设备本地语言而不是上午/下午给出

问题描述

 Date currentDate = Calendar.getInstance().getTime();
 String time = new SimpleDateFormat("HH:mm a").format(currentDate);

现在我的时间是中午 11:40 而不是上午 11:40。如何避免在我的应用程序中使用设备默认语言。

标签: javaandroid

解决方案


使用以 Locale 作为参数的构造函数。检查 https://docs.oracle.com/javase/jp/8/docs/api/java/text/SimpleDateFormat.html#SimpleDateFormat-java.lang.String-java.util.Locale-

您的代码将如下所示:

time = new SimpleDateFormat("HH:mm a",Locale.ENGLISH).format(currentDate);

推荐阅读