0

How can I get the time an action was performed in the users preset format (24/12Hr)? I'm trying to set a textview with the time an onClick was performed.

EDIT: I've tried

Time currentTime = new Time();
currentTime.setToNow();

However, instead of XX:XX/X:XX I get a bunch of unneeded text.

Carl
  • 249
  • 2
  • 5
  • 13
  • possible duplicate of [Get current time and date on Android](http://stackoverflow.com/questions/5369682/get-current-time-and-date-on-android) – Andrew Schuster Aug 07 '14 at 13:29

1 Answers1

1

The question have been asked already, e.g. here. So, to put current date-time in TextView use the following code:

time.setText(DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()));

If You need only time (without date), then it should look the following:

time.setText(DateFormat.getTimeInstance().format(Calendar.getInstance().getTime()))

Also, checkout getTimeInstance(int style) which allows to use different style of the format (e.g. long, short, medium).

Community
  • 1
  • 1
sandrstar
  • 12,503
  • 8
  • 58
  • 65