Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 826
liya Avatar asked Nov 26 '13 09:11

liya


2 Answers

try following code

     Date d=new Date();
     SimpleDateFormat sdf=new SimpleDateFormat("hh:mm a");
     String currentDateTimeString = sdf.format(d);
     tv.setText(currentDateTimeString);
like image 200
Ketan Ahir Avatar answered Oct 16 '22 02:10

Ketan Ahir


It gives the current date, month, year and time, but I need to get only the current time from this.

If you want to do this and respect the users preferences (e.g. 12 vs 24 hour clock) then continue to use DateFormat, only like so:

DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date());
like image 38
weston Avatar answered Oct 16 '22 03:10

weston