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()]
try following code
Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm a");
String currentDateTimeString = sdf.format(d);
tv.setText(currentDateTimeString);
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With