Search code examples
javaandroiddate-formatdate-formatting

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


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()]


Solution

  • try following code

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