如何在Android中格式化日期和时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/454315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to format date and time in Android?
提问by pupeno
How to format correctly according to the device configuration date and time when having a year, month, day, hour and minute?
有年、月、日、时、分时,如何根据设备配置日期和时间正确格式化?
采纳答案by JamieH
Use the standard Java DateFormat class.
使用标准的 Java DateFormat 类。
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));
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.
您可以使用自己的值初始化 Date 对象,但是您应该知道构造函数已被弃用,您应该真正使用 Java Calendar 对象。
回答by Fuangwith S.
In my opinion, android.text.format.DateFormat.getDateFormat(context)
makes me confused because this method returns java.text.DateFormat
rather than android.text.format.DateFormat
- -".
在我看来,android.text.format.DateFormat.getDateFormat(context)
让我感到困惑,因为这个方法返回java.text.DateFormat
而不是android.text.format.DateFormat
- -”。
So, I use the fragment code as below to get the current date/time in my format.
因此,我使用如下片段代码以我的格式获取当前日期/时间。
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());
In addition, you can use others formats. Follow DateFormat.
此外,您可以使用其他格式。遵循DateFormat。
回答by Dany Pop
You can use DateFormat
. Result depends on default Locale of the phone, but you can specify Locale too :
您可以使用DateFormat
. 结果取决于手机的默认语言环境,但您也可以指定语言环境:
https://developer.android.com/reference/java/text/DateFormat.html
https://developer.android.com/reference/java/text/DateFormat.html
This is results on a
这是一个结果
DateFormat.getDateInstance().format(date)
FR Locale : 3 nov. 2017
FR 语言环境:11 月 3 日 2017年
US/En Locale : Jan 12, 1952
US/En 语言环境:1952 年 1 月 12 日
DateFormat.getDateInstance(DateFormat.SHORT).format(date)
FR Locale : 03/11/2017
FR 语言环境 : 03/11/2017
US/En Locale : 12.13.52
美国/En 语言环境:12.13.52
DateFormat.getDateInstance(DateFormat.MEDIUM).format(date)
FR Locale : 3 nov. 2017
FR 语言环境:11 月 3 日 2017年
US/En Locale : Jan 12, 1952
US/En 语言环境:1952 年 1 月 12 日
DateFormat.getDateInstance(DateFormat.LONG).format(date)
FR Locale : 3 novembre 2017
FR 语言环境:2017 年 11 月 3 日
US/En Locale : January 12, 1952
美国/En 语言环境:1952 年 1 月 12 日
DateFormat.getDateInstance(DateFormat.FULL).format(date)
FR Locale : vendredi 3 novembre 2017
FR 语言环境:vendredi 2017 年 11 月 3 日
US/En Locale : Tuesday, April 12, 1952
US/En 语言环境:1952 年 4 月 12 日,星期二
DateFormat.getDateTimeInstance().format(date)
FR Locale : 3 nov. 2017 16:04:58
FR 语言环境:11 月 3 日 2017 16:04:58
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date)
FR Locale : 03/11/2017 16:04
FR 语言环境 : 03/11/2017 16:04
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(date)
FR Locale : 03/11/2017 16:04:58
FR 语言环境 : 03/11/2017 16:04:58
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG).format(date)
FR Locale : 03/11/2017 16:04:58 GMT+01:00
FR 语言环境 : 03/11/2017 16:04:58 GMT+01:00
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL).format(date)
FR Locale : 03/11/2017 16:04:58 heure normale d'Europe centrale
FR Locale : 03/11/2017 16:04:58 heure normale d'Europe centrale
DateFormat.getTimeInstance().format(date)
FR Locale : 16:04:58
FR 语言环境 : 16:04:58
DateFormat.getTimeInstance(DateFormat.SHORT).format(date)
FR Locale : 16:04
FR 语言环境 : 16:04
DateFormat.getTimeInstance(DateFormat.MEDIUM).format(date)
FR Locale : 16:04:58
FR 语言环境 : 16:04:58
DateFormat.getTimeInstance(DateFormat.LONG).format(date)
FR Locale : 16:04:58 GMT+01:00
FR 语言环境:16:04:58 GMT+01:00
DateFormat.getTimeInstance(DateFormat.FULL).format(date)
FR Locale : 16:04:58 heure normale d'Europe centrale
FR Locale : 16:04:58 heure normale d'Europe centrale
回答by Ivo Stoyanov
Date to Locale date string:
日期到语言环境日期字符串:
Date date = new Date();
String stringDate = DateFormat.getDateTimeInstance().format(date);
Options:
选项:
DateFormat.getDateInstance()
- > Dec 31, 1969
- > 1969 年 12 月 31 日
DateFormat.getDateTimeInstance()
-> Dec 31, 1969 4:00:00 PM
-> 1969 年 12 月 31 日下午 4:00:00
DateFormat.getTimeInstance()
-> 4:00:00 PM
-> 下午 4:00:00
回答by tronman
This will do it:
这将做到:
Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));
回答by neknek mouh
Use SimpleDateFormat
使用 SimpleDateFormat
Like this:
像这样:
event.putExtra("starttime", "12/18/2012");
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date date = format.parse(bundle.getString("starttime"));
回答by FireZenk
Following this: http://developer.android.com/reference/android/text/format/Time.html
在此之后:http: //developer.android.com/reference/android/text/format/Time.html
Is better to use Android native Time class:
最好使用 Android 原生 Time 类:
Time now = new Time();
now.setToNow();
Then format:
然后格式化:
Log.d("DEBUG", "Time "+now.format("%d.%m.%Y %H.%M.%S"));
回答by FireZenk
Use these two as a class variables:
使用这两个作为类变量:
public java.text.DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
private Calendar mDate = null;
And use it like this:
并像这样使用它:
mDate = Calendar.getInstance();
mDate.set(year,months,day);
dateFormat.format(mDate.getTime());
回答by Jorgesys
This is my method, you can define and input and output format.
这是我的方法,你可以定义和输入输出格式。
public static String formattedDateFromString(String inputFormat, String outputFormat, String inputDate){
if(inputFormat.equals("")){ // if inputFormat = "", set a default input format.
inputFormat = "yyyy-MM-dd hh:mm:ss";
}
if(outputFormat.equals("")){
outputFormat = "EEEE d 'de' MMMM 'del' yyyy"; // if inputFormat = "", set a default output format.
}
Date parsed = null;
String outputDate = "";
SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, java.util.Locale.getDefault());
SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, java.util.Locale.getDefault());
// You can set a different Locale, This example set a locale of Country Mexico.
//SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, new Locale("es", "MX"));
//SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, new Locale("es", "MX"));
try {
parsed = df_input.parse(inputDate);
outputDate = df_output.format(parsed);
} catch (Exception e) {
Log.e("formattedDateFromString", "Exception in formateDateFromstring(): " + e.getMessage());
}
return outputDate;
}
回答by Tomas
SimpleDateFormat
简单日期格式
I use SimpleDateFormat without custom patternto get actual date and time from the system in the device's preselectedformat:
我使用没有自定义模式的SimpleDateFormat以设备的预选格式从系统获取实际日期和时间:
public static String getFormattedDate() {
//SimpleDateFormat called without pattern
return new SimpleDateFormat().format(Calendar.getInstance().getTime());
}
returns:
返回:
- 13.01.15 11:45
- 1/13/15 10:45 AM
- ...
- 13.01.15 11:45
- 15 年 1 月 13 日上午 10:45
- ...