如何从DateTime格式获取日,月,年,小时,分,秒? [英] How to get day, month, year and hour, minutes, second from DateTime format?

查看:277
本文介绍了如何从DateTime格式获取日,月,年,小时,分,秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过以下代码,但在Android中已经弃用了

I tried this following code but its a deprecated in android

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

try {

    Date date1 = simpleDateFormat.parse("11/20/2014 8:10:00 AM");

    Log.e("date", "" + date1.getYear());

    Log.e("month", "" + date1.getmonth());

    Log.e("year", "" + date1.getYear());

    Log.e("hour", "" + date1.getHours());

    Log.e("minutes", "" + date1.getMinutes());

    Log.e("seconds", "" + date1.getSeconds());

} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

它已被弃用如何使用日历并获得日,月,年? / p>

Its is deprecated how to use calendar and get day, month, year?

推荐答案

使用simpleDateFormat.parse()设置日历时间,并从日历中获取字段值:

Set calendar time using simpleDateFormat.parse() and get field value from calendar :

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Calendar calendar = Calendar.getInstance();
try {
     calendar.setTime(simpleDateFormat.parse("11/20/2014 8:10:00 AM"));

     Log.e("date", "" + calendar.get(Calendar.DAY_OF_MONTH));

     Log.e("month", ""+calendar.get(Calendar.MONTH));

     Log.e("year", ""+calendar.get(Calendar.YEAR));

     Log.e("hour", ""+calendar.get(Calendar.HOUR));

     Log.e("minutes", ""+calendar.get(Calendar.MINUTE));

     Log.e("seconds", ""+calendar.get(Calendar.SECOND));
} catch (ParseException e) {
   e.printStackTrace();
}

这篇关于如何从DateTime格式获取日,月,年,小时,分,秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆