在有年、月、日、时、分的情况下,如何根据设备配置的日期和时间正确格式化?
当前回答
地方
以毫秒格式获取日期或时间,我使用了这个:
日期和时间
Date date = new Date(milliseconds);
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.getDefault());
dateFormat.format(date);
Date
Date date = new Date(milliseconds);
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
dateFormat.format(date);
Time
Date date = new Date(milliseconds);
DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
dateFormat.format(date);
您可以使用其他日期样式和时间样式。更多关于样式的信息。
其他回答
您可以使用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
Date to Locale日期字符串:
Date date = new Date();
String stringDate = DateFormat.getDateTimeInstance().format(date);
选项:
DateFormat.getDateInstance()
- > 1969年12月31日
DateFormat.getDateTimeInstance()
-> 1969年12月31日下午4:00:00
DateFormat.getTimeInstance()
->下午4:00:00
最短的方法:
// 2019-03-29 16:11
String.format("%1$tY-%<tm-%<td %<tR", Calendar.getInstance())
%tR是%tH:%tM的缩写,<表示重用最后一个参数(1$)。
它等价于String。格式(“% 1 $ tY - % 1 $ tm - td % 1 $ % 1 $ tH: % 1 $ tm”,Calendar.getInstance ())
https://developer.android.com/reference/java/util/Formatter.html
这是我的方法,可以定义和输入输出格式。
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;
}
简单日期格式
我使用SimpleDateFormat没有自定义模式,以设备的预选格式从系统中获得实际的日期和时间:
public static String getFormattedDate() {
//SimpleDateFormat called without pattern
return new SimpleDateFormat().format(Calendar.getInstance().getTime());
}
返回:
13.01.15 11 1/13/15上午10:45 ...
推荐文章
- 警告: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中转换字符串到日历对象