本文介绍了如何在Android中格式化日期和时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当具有年,月,日,小时和分钟时,如何根据设备配置日期和时间正确格式化?
How to format correctly according to the device configuration date and time when having a year, month, day, hour and minute?
推荐答案
使用标准的Java DateFormat类.
Use the standard Java DateFormat class.
例如,要显示当前日期和时间,请执行以下操作:
For example to display the current date and time do the following:
Date date = new Date(location.getTime());
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));
您可以使用自己的值初始化Date对象,但是您应该意识到构造函数已被弃用,并且您确实应该使用Java Calendar对象.
You can initialise a Date object with your own values, however you should be aware that the constructors have been deprecated and you should really be using a Java Calendar object.
这篇关于如何在Android中格式化日期和时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!