在有年、月、日、时、分的情况下,如何根据设备配置的日期和时间正确格式化?
当前回答
您可以使用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
其他回答
日期和时间格式说明
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 )
这是最简单的方法:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a", Locale.US);
String time = df.format(new Date());
如果你在寻找模式,检查这个 https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
The other answers are generally correct. I should like to contribute the modern answer. The classes Date, DateFormat and SimpleDateFormat used in most of the other answers, are long outdated and have caused trouble for many programmers over many years. Today we have so much better in java.time, AKA JSR-310, the modern Java date & time API. Can you use this on Android yet? Most certainly! The modern classes have been backported to Android in the ThreeTenABP project. See this question: How to use ThreeTenABP in Android Project for all the details.
这段代码应该让你开始:
int year = 2017, month = 9, day = 28, hour = 22, minute = 45;
LocalDateTime dateTime = LocalDateTime.of(year, month, day, hour, minute);
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
System.out.println(dateTime.format(formatter));
当我将电脑的首选语言设置为美式英语或英式英语时,打印如下:
Sep 28, 2017 10:45:00 PM
当我把它设置为丹麦语时,我得到:
28-09-2017 22:45:00
所以它符合构型。不过,我不确定它具体跟踪了你设备的日期和时间设置的哪些细节,而且这可能因手机而异。
使用标准的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对象。
这样就可以了:
Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- 解析日期字符串并更改格式
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- Java SimpleDateFormat("yyyy-MM-dd' t ' hh:mm:ss' z '")给出的时区为IST
- 在Java中转换字符串到日历对象