How to display only current time using DateFormat.getDateTimeInstance() in android

liya picture liya · Nov 26, 2013 · Viewed 13.6k times · Source

I am new to android. In android I want to display only current time in a textview in the format hh:mm am/pm (eg: 3:02 PM) I use the following code.

String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date()); tv.setText(currentDateTimeString);

It gives the current date,month,year and time.But i need to get only the current time from this. Is there any way to display only time? Can anyone help me to get the time without date from the above code.[using DateFormat.getDateTimeInstance()]

Answer

Ketan Ahir picture Ketan Ahir · Nov 26, 2013

try following code

     Date d=new Date();
     SimpleDateFormat sdf=new SimpleDateFormat("hh:mm a");
     String currentDateTimeString = sdf.format(d);
     tv.setText(currentDateTimeString);