-5

I have Date like this Sun May 20 09:18:44 GMT+04:30 2018 how i can get clock from this Date, i want result like this 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
amir dt
  • 83
  • 2
  • 9

2 Answers2

1

Try this code..

        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

your code is working you just have to initialize Date class's object Date date = new 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);
Zezariya Nilesh
  • 390
  • 2
  • 14