5

How do I format time using Android's Time Class. I want to display time in this format "hh:mm AM/PM" . I have tried using the Time.format function but I'm not sure if I'm using it correctly.

Thanks

Mahadevan Sreenivasan
  • 1,144
  • 1
  • 9
  • 26
  • May be [this](http://stackoverflow.com/questions/454315/how-do-you-format-date-and-time-in-android) is helpful. – Mudassir Jun 24 '11 at 07:13
  • 1
    [Do not use the Time class](https://code.google.com/p/android/issues/detail?id=76439). It's going to be removed in the future and has many issues with it. – Sandy Chapman Oct 21 '14 at 14:29

3 Answers3

10

Please try this..

SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm aa");
Date dt = new Date();
String strValue = timeFormat.format(dt);
Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94
Nikhil
  • 16,194
  • 20
  • 64
  • 81
7
Time time = new Time();
time.set(0, 0, 17, 4, 5, 1999);
Log.i("Time", time.format("%d.%m.%Y %H:%M:%S"));
Igor Krumpak
  • 163
  • 2
  • 2
  • [Do not use the Time class](https://code.google.com/p/android/issues/detail?id=76439). It's going to be removed in the future and has many issues with it. – Sandy Chapman Oct 21 '14 at 14:28
4

try:

String strTime = time.format("%I:%M %p");

Your answer can be derived from, but not limited to this link, a C++ reference about "ctime": http://www.cplusplus.com/reference/ctime/strftime/ I found this link to be very helpful deciphering the string formats used in my own work.

The Android.Text.Format.Time.Format docs assumes you know something: Where or how to read "man" page for strftime, which if you weren't familiar using Linux (or a Mac at the terminal) might require some creative web searching or know what "See man strftime for what means what." referred to. Informal as it is in official documentation, it does build off and reference what has already has come before and is left as an exercise for the developer.

Hunter-Orionnoir
  • 2,015
  • 1
  • 18
  • 23
  • 1
    [Do not use the Time class](https://code.google.com/p/android/issues/detail?id=76439). It's going to be removed in the future and has many issues with it. – Sandy Chapman Oct 21 '14 at 14:29