0

In an Android app I have an internal logging method and I add a time-date stamp to the start of each message . . .

public void logEvent(String sMsg)  {
        String delegate = "MM/dd/yy hh:mm:ss"; 
        java.util.Date noteTS = Calendar.getInstance().getTime();
        String sTod = " " + DateFormat.format(delegate,noteTS);
        sMsg = sTod + "   " + sMsg;
        logEvents.add(sMsg); 
 . . . 

This produces a time-date stamp that looks like "07/25/14 02:58:18". But I want the time to be in 24 hour format, i.e., "14:58:18".

In some systems that's accomplished by using "HH" instead of "hh" so I tried that, i.e.,

   String delegate = "MM/dd/yy HH:mm:ss";

... but that just gave me "07/25/14 HH:58:18"

So what do I have to do to get the hours to be in 24-hour format?

user316117
  • 7,971
  • 20
  • 83
  • 158

3 Answers3

2

Use SimpleDateFormat instead of DateFormat. Refer SimpleDateFormat Javadoc API: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

H   Hour in day (0-23)  Number  0

"MM/dd/yy HH:mm:ss" should work with SimpleDateFormat.

    SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
    java.util.Date noteTS = Calendar.getInstance().getTime();
    String sTod = formatter.format(noteTS);
Pat
  • 2,223
  • 4
  • 19
  • 25
1

Try this:

private final SimpleDateFormat sdfTime = new SimpleDateFormat("kk:mm");

...
TEXTVIEW.setTitle(sdfTime.format(new Date()));

http://developer.android.com/reference/java/text/SimpleDateFormat.html

nouseforname
  • 720
  • 1
  • 5
  • 17
0

You can use very simple class for it.

Try this:

Time time=new Time();

time.setToNow();

textview.setText(time.hour+":"+time.minute);

Hope be useful for you.:)