Android Date.toLocaleString() - 没有时间元素?

Mik*_*ike 3 datetime android

我在我的应用程序中使用Date.toLocaleString()来获取日期的字符串表示 - 我只想要没有时间元素的日期(即2012年4月5日),这可能吗?

然而,我实现这一点,我需要它是多语言所以'5/4/2012'是不好的,因为美国的日期是MM/DD/YYYY,而不是英国的DD/MM/YYYY.

谢谢

Jon*_*eet 7

文档中所述,此方法已弃用.相反,您应该使用DateFormat(例如SimpleDateFormat或内置的),这样可以轻松指定日期模式.

例如:

// You can specify styles if you want
DateFormat format = DateFormat.getDateInstance();
// Set time zone information if you want.
Date date = ...;
String text = format.format(date);
Run Code Online (Sandbox Code Playgroud)