在有年、月、日、时、分的情况下,如何根据设备配置的日期和时间正确格式化?
当前回答
避免j.u.Date
Java(和Android)中的Java.util. date和. calendar和SimpleDateFormat是出了名的麻烦。避免它们。它们太糟糕了,以至于Sun/Oracle放弃了它们,用新的java取代了它们。Java 8中的time包(2014年Android中没有)。新的java。time的灵感来自Joda-Time图书馆。
乔达时间
Joda-Time在Android上运行。
在StackOverflow上搜索“Joda”可以找到很多例子和很多讨论。
使用Joda-Time 2.4的一小段源代码。
标准格式。
String output = DateTime.now().toString();
// Current date-time in user's default time zone with a String representation formatted to the ISO 8601 standard.
本地化的格式。
String output = DateTimeFormat.forStyle( "FF" ).print( DateTime.now() );
// Full (long) format localized for this user's language and culture.
其他回答
这个代码为我工作!
Date d = new Date();
CharSequence s = android.text.format.DateFormat.format("MM-dd-yy hh-mm-ss",d.getTime());
Toast.makeText(this,s.toString(),Toast.LENGTH_SHORT).show();
使用Time类中的构建!
Time time = new Time();
time.set(0, 0, 17, 4, 5, 1999);
Log.i("DateTime", time.format("%d.%m.%Y %H:%M:%S"));
您可以使用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
这是最简单的方法:
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
我是这样使用的:
public class DateUtils {
static DateUtils instance;
private final DateFormat dateFormat;
private final DateFormat timeFormat;
private DateUtils() {
dateFormat = android.text.format.DateFormat.getDateFormat(MainApplication.context);
timeFormat = android.text.format.DateFormat.getTimeFormat(MainApplication.context);
}
public static DateUtils getInstance() {
if (instance == null) {
instance = new DateUtils();
}
return instance;
}
public synchronized static String formatDateTime(long timestamp) {
long milliseconds = timestamp * 1000;
Date dateTime = new Date(milliseconds);
String date = getInstance().dateFormat.format(dateTime);
String time = getInstance().timeFormat.format(dateTime);
return date + " " + time;
}
}
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- 如何减去日期/时间在JavaScript?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 自定义数字格式字符串始终显示符号
- 导航目标xxx对于这个NavController是未知的