我的要求是根据特定的locale
.
我编写了一个 Java 程序来根据某些语言环境格式化当前时间,它给出的输出如下所示:
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class DateFormatDemo {
static public void showTimeStyles(Locale currentLocale) {
Date today = new Date();
String result;
DateFormat formatter;
int[] styles = { DateFormat.DEFAULT, DateFormat.SHORT, DateFormat.MEDIUM,
DateFormat.LONG, DateFormat.FULL };
System.out.println();
System.out.println("Locale: " + currentLocale.toString());
System.out.println();
for (int k = 0; k < styles.length; k++) {
formatter = DateFormat.getTimeInstance(styles[k], currentLocale);
result = formatter.format(today);
System.out.println(result);
}
}
static public void main(String[] args) {
showTimeStyles(new Locale("en", "US"));
showTimeStyles(new Locale("fr", "FR"));
showTimeStyles(new Locale("de", "DE"));
}
}
上述 Java 程序的输出为:
Locale: en_US
9:49:37 AM
9:49 AM
9:49:37 AM
9:49:37 AM UTC
9:49:37 AM UTC
Locale: fr_FR
09:49:37
09:49
09:49:37
09:49:37 UTC
09 h 49 UTC
Locale: de_DE
09:49:37
09:49
09:49:37
09:49:37 UTC
09:49 Uhr UTC
因此,基本上,对于 en-US,时间格式为 in 12-hour format
,对于 fr-FR,时间格式为24-hours format
.
但是,当我在 Android 应用程序中使用相同的代码时,如下所示:
public void showTimeStyles(Locale currentLocale) {
Date today = new Date();
String result;
DateFormat formatter;
int[] styles = { DateFormat.DEFAULT, DateFormat.SHORT, DateFormat.MEDIUM,
DateFormat.LONG, DateFormat.FULL };
Log.d("MY_APP","Locale: " + currentLocale.toString());
for (int k = 0; k < styles.length; k++) {
formatter = DateFormat.getTimeInstance(styles[k], currentLocale);
result = formatter.format(today);
Log.d("MY_APP",result);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
final View rootView = inflater.inflate(R.layout.fragment_home, container, false);
showTimeStyles(new Locale("en", "US"));
showTimeStyles(new Locale("fr", "FR"));
showTimeStyles(new Locale("de", "DE"));
return rootView;
}
上述android程序的输出如下图所示:
D/MY_APP: Locale: en_US
D/MY_APP: 3:28:19 PM
D/MY_APP: 3:28 PM
D/MY_APP: 3:28:19 PM
D/MY_APP: 3:28:19 PM GMT+05:30
D/MY_APP: 3:28:19 PM India Standard Time
D/MY_APP: Locale: fr_FR
D/MY_APP: 3:28:19 PM
D/MY_APP: 3:28 PM
D/MY_APP: 3:28:19 PM
D/MY_APP: 15:28:19 GMT+05:30
D/MY_APP: 15:28:19 heure de l’Inde
D/MY_APP: Locale: de_DE
D/MY_APP: 3:28:19 nachm.
D/MY_APP: 3:28 nachm.
D/MY_APP: 3:28:19 nachm.
D/MY_APP: 15:28:19 GMT+05:30
D/MY_APP: 15:28:19 Indische Zeit
现在,如果我们比较 Java 代码和 android 代码的输出,对于语言环境“fr-FR”,时间在 Android 中以 12 小时格式显示,在 Java 中以 24 小时格式显示。
正确的输出是我们从 Java 代码中得到的输出。也就是说,在法语中,标准时间格式是 24 小时格式,而不是带有 AM/PM 的 12 小时格式。
为什么代码行为不同?
我们如何在 android 中为特定语言环境获取本地化时间格式?
是否有可能在Joda-Time
Android 中获得本地化时间格式,如果可以,如何?
使用Joda-time
我尝试了以下代码,但“fr-FR”的输出仍为 12 小时格式:
//Convert the received timestamp to Joda DateTime
DateTime dt = new DateTime(date);
// translate the received DateTime to the time in the User's Time Zone
DateTime dateInUserTimeZone = dt.withZone(DateTimeZone.forID(User.getInstance().getTimeZone()));
String pattern = DateTimeFormat.patternForStyle("MM", new Locale("fr","FR"));
DateTimeFormatter formatterOutput1 = DateTimeFormat.forPattern(pattern);
Log.d("MY_APP","DateTime:"+formatterOutput1.print(dateInUserTimeZone));