如何从约会中获取时钟

时间:2018-05-21 05:18:35

标签: java android date

我有Date这样Sun May 20 09:18:44 GMT+04:30 2018我如何从Date获取时钟, 我想要这样的结果09:18 am

Log.v(TAG,"date "+ date.toString());// output is Sun May 20 09:18:44 GMT+04:30 2018
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm");
String time=sdf.format(date);//NullPointerException

2 个答案:

答案 0 :(得分:1)

试试这段代码..

        SimpleDateFormat sdf3 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
    SimpleDateFormat sdf4 = new SimpleDateFormat("hh:mm", Locale.ENGLISH);

    Date d1 = null;
    try{
        d1 = sdf3.parse("Sun May 20 09:18:44 GMT+04:30 2018");
        String date=sdf4.format(d1);
        System.out.println("time..." + date);

    }catch (Exception e){ e.printStackTrace(); }


    System.out.println("check..." + d1);

答案 1 :(得分:1)

您的代码正常工作您只需要初始化Date类的对象           日期日期=新日期();

    Date date = new Date();
    Log.v(TAG,"date "+ date.toString());
    SimpleDateFormat sdf=new SimpleDateFormat("hh:mm");
    String time=sdf.format(date);
    Log.v(TAG,"date "+ time);