如何根据设备配置的日期和时间正确格式化具有年、月、日、小时和分钟的时间?
如何根据设备配置的日期和时间正确格式化具有年、月、日、小时和分钟的时间?
使用标准的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对象。
我认为android.text.format.DateFormat.getDateFormat(context)
这个方法让我感到困惑,因为它返回的是java.text.DateFormat
而不是android.text.format.DateFormat
- -"。
所以,我使用以下代码片段以我的格式获取当前日期/时间。
android.text.format.DateFormat df = new android.text.format.DateFormat();
df.format("yyyy-MM-dd hh:mm:ss a", new java.util.Date());
or
android.text.format.DateFormat.format("yyyy-MM-dd hh:mm:ss a", new java.util.Date());
此外,您可以使用其他格式。请参考DateFormat。
java.text.SimpleDateFormat
(你提供的链接,使用 H
表示 0-23 范围内的小时)和 android.text.format.DateFormat
(回答所涉及的内容,使用 k
表示 0-23 范围内的小时)之间是有区别的。 - dnetDateFormat
。结果取决于手机的默认区域设置,但你也可以指定区域设置:
https://developer.android.com/reference/java/text/DateFormat.html
这是在一个中文环境下的结果:
DateFormat.getDateInstance().format(date)
法国本地时间: 2017年11月3日
美国/英语本地时间: 1952年1月12日
DateFormat.getDateInstance(DateFormat.SHORT).format(date)
法国地区:2017年3月11日
美国/英国地区:12:13:52
DateFormat.getDateInstance(DateFormat.MEDIUM).format(date)
法国本地日期:2017年11月3日
美国/英语本地日期:1952年1月12日
DateFormat.getDateInstance(DateFormat.LONG).format(date)
FR本地时间:2017年11月3日
US/En本地时间:1952年1月12日
DateFormat.getDateInstance(DateFormat.FULL).format(date)
FR语言环境:2017年11月3日星期五
US/En语言环境:1952年4月12日星期二
DateFormat.getDateTimeInstance().format(date)
法国本地时间:2017年11月3日16:04:58
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date)
法国本地化:2017年03月11日 16:04
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(date)
法语区域设置:03/11/2017 16:04:58
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG).format(date)
法国地区:2017年03月11日 16:04:58 GMT+01:00
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL).format(date)
法国本地时间:2017年03月11日16:04:58欧洲中部标准时间
DateFormat.getTimeInstance().format(date)
法国本地时间:16:04:58
DateFormat.getTimeInstance(DateFormat.SHORT).format(date)
法语区域设置:16:04
DateFormat.getTimeInstance(DateFormat.MEDIUM).format(date)
法国本地时间:16:04:58
DateFormat.getTimeInstance(DateFormat.LONG).format(date)
法国语言环境:16:04:58 GMT+01:00
DateFormat.getTimeInstance(DateFormat.FULL).format(date)
法语区域设置:欧洲中部标准时间16:04:58
DateFormat.getPatternInstance("dMMMMyyyyHHmm", Locale.ENGLISH).format(date)
-> December 23, 2005, 17:37
。 - Nikolay Krasilnikov将日期转换为本地日期字符串:
Date date = new Date();
String stringDate = DateFormat.getDateTimeInstance().format(date);
选项:
DateFormat.getDateInstance()
DateFormat.getDateTimeInstance()
-> 1969年12月31日 下午4:00:00
DateFormat.getTimeInstance()
-> 下午4:00:00
EEE : Day ( Mon )
MMMM : Full month name ( December ) // MMMM February
MMM : Month in words ( Dec )
MM : Month ( 12 )
dd : Day in 2 chars ( 03 )
d: Day in 1 char (3)
HH : Hours ( 12 )
mm : Minutes ( 50 )
ss : Seconds ( 34 )
yyyy: Year ( 2020 ) //both yyyy and YYYY are same
YYYY: Year ( 2020 )
zzz : GMT+05:30
a : ( AM / PM )
aa : ( AM / PM )
aaa : ( AM / PM )
aaaa : ( AM / PM )
下面这个代码可以实现此功能:
Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));
使用SimpleDateFormat
像这样:
event.putExtra("starttime", "12/18/2012");
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date date = format.parse(bundle.getString("starttime"));
new SimpleDateFormat("my-format", Locale.getDefault());
- iutinvg这是最简单的方法:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a", Locale.US);
String time = df.format(new 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"));
将这两个变量作为类变量使用:
public java.text.DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
private Calendar mDate = null;
然后像这样使用:
mDate = Calendar.getInstance();
mDate.set(year,months,day);
dateFormat.format(mDate.getTime());
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
返回的dateFormat
是类型为java.text.DateFormat
(而不是 `android.text.format.DateFormat')的对象。 - Arye Rosenstein