在有年、月、日、时、分的情况下,如何根据设备配置的日期和时间正确格式化?


当前回答

这样就可以了:

Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));

其他回答

下面是:http://developer.android.com/reference/android/text/format/Time.html

最好使用Android原生的Time类:

Time now = new Time();
now.setToNow();

然后格式:

Log.d("DEBUG", "Time "+now.format("%d.%m.%Y %H.%M.%S"));

您可以使用DateFormat。结果取决于手机的默认Locale,但你也可以指定Locale:

https://developer.android.com/reference/java/text/DateFormat.html

这是a的结果

DateFormat.getDateInstance().format(date)                                          

地点:2017年11月3日

美国/En当地:Jan 12, 1952


DateFormat.getDateInstance(DateFormat.SHORT).format(date)

地点:03/11/2017

美国和地区:12.13.52


DateFormat.getDateInstance(DateFormat.MEDIUM).format(date)

地点:2017年11月3日

美国/En当地:Jan 12, 1952


DateFormat.getDateInstance(DateFormat.LONG).format(date)

当地时间:2017年11月3日

美国/现场:1952年1月12日


DateFormat.getDateInstance(DateFormat.FULL).format(date)

当地时间:2017年11月3日星期五

美国/现场:1952年4月12日,星期二


DateFormat.getDateTimeInstance().format(date)

FR地点:2017年11月3日16:04:58


DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date)

FR地区:03/11/2017 16:04


DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(date)

本地:03/11/2017 16:04:58


DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG).format(date)

本地FR: 03/11/2017 16:04:58 GMT+01:00


DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL).format(date)

当地时间:中欧标准时间03/11/2017 16:04:58


DateFormat.getTimeInstance().format(date)

本地FR: 16:04:58


DateFormat.getTimeInstance(DateFormat.SHORT).format(date)

本地FR: 16:04


DateFormat.getTimeInstance(DateFormat.MEDIUM).format(date)

本地FR: 16:04:58


DateFormat.getTimeInstance(DateFormat.LONG).format(date)

本地FR:格林尼治时间16:04:58 +01:00


DateFormat.getTimeInstance(DateFormat.FULL).format(date)

当地时间:中欧标准时间16:04:58


避免j.u.Date

Java(和Android)中的Java.util. date和. calendar和SimpleDateFormat是出了名的麻烦。避免它们。它们太糟糕了,以至于Sun/Oracle放弃了它们,用新的java取代了它们。Java 8中的time包(2014年Android中没有)。新的java。time的灵感来自Joda-Time图书馆。

乔达时间

Joda-Time在Android上运行。

在StackOverflow上搜索“Joda”可以找到很多例子和很多讨论。

使用Joda-Time 2.4的一小段源代码。

标准格式。

String output = DateTime.now().toString(); 
// Current date-time in user's default time zone with a String representation formatted to the ISO 8601 standard.

本地化的格式。

String output = DateTimeFormat.forStyle( "FF" ).print( DateTime.now() ); 
// Full (long) format localized for this user's language and culture.

这样就可以了:

Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));

使用标准的Java DateFormat类。

例如,要显示当前的日期和时间,请执行以下操作:

Date date = new Date(location.getTime());
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));

你可以用你自己的值初始化一个Date对象,但是你应该知道构造函数已经被弃用了,你应该真正使用一个Java Calendar对象。