在有年、月、日、时、分的情况下,如何根据设备配置的日期和时间正确格式化?
当前回答
android Time类提供了3种格式化方法http://developer.android.com/reference/android/text/format/Time.html
我是这样做的:
/**
* This method will format the data from the android Time class (eg. myTime.setToNow()) into the format
* Date: dd.mm.yy Time: hh.mm.ss
*/
private String formatTime(String time)
{
String fullTime= "";
String[] sa = new String[2];
if(time.length()>1)
{
Time t = new Time(Time.getCurrentTimezone());
t.parse(time);
// or t.setToNow();
String formattedTime = t.format("%d.%m.%Y %H.%M.%S");
int x = 0;
for(String s : formattedTime.split("\\s",2))
{
System.out.println("Value = " + s);
sa[x] = s;
x++;
}
fullTime = "Date: " + sa[0] + " Time: " + sa[1];
}
else{
fullTime = "No time data";
}
return fullTime;
}
我希望这对你有帮助:-)
其他回答
您可以使用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 date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));
回到2016年,当我想自定义格式(不是根据设备配置,如你所问…)我通常使用字符串资源文件:
在strings.xml:
<string name="myDateFormat"><xliff:g id="myDateFormat">%1$td/%1$tm/%1$tY</xliff:g></string>
在活动:
Log.d(TAG, "my custom date format: "+getString(R.string.myDateFormat, new Date()));
这对于新的日期绑定库的发布也很有用。
我可以在布局文件中有这样的东西:
<TextView
android:id="@+id/text_release_date"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="2dp"
android:text="@{@string/myDateFormat(vm.releaseDate)}"
tools:text="0000"
/>
在java类中:
MovieDetailViewModel vm = new MovieDetailViewModel();
vm.setReleaseDate(new Date());
在我看来,android.text.format.DateFormat. getdateformat (context)让我感到困惑,因为这个方法返回java.text.DateFormat而不是android.text.format.DateFormat - -"。
因此,我使用下面的片段代码以我的格式获取当前日期/时间。
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());
此外,您还可以使用其他格式。遵循DateFormat。
使用Time类中的构建!
Time time = new Time();
time.set(0, 0, 17, 4, 5, 1999);
Log.i("DateTime", time.format("%d.%m.%Y %H:%M:%S"));
推荐文章
- 警告: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中转换字符串到日历对象