Search code examples
javajava-melwuit

Time in hour:Minute:second format in j2me


I trying to make a method that return a String that expresses of current time in j2me or lwuit framework at hour:Minute:Second Format.

If I use the following code the output will be:

Wed Mar 06 13:55:45 GMT+03:00 2013 (only I need 13:55:45)

public String   Refresh() {
    java.util.Date t = new java.util.Date(System.currentTimeMillis() );
    String tt = t.toString();
    return tt;
}

And if I use the following code Refresh method always returns 3:0:0 !!!

java.util.Calendar calendar;
public String   Refresh() {
     calendar = calendar.getInstance();
     StringBuffer time = new StringBuffer();
     time.append(calendar.get(java.util.Calendar.HOUR_OF_DAY)).append(':');
     time.append(calendar.get(java.util.Calendar.MINUTE)).append(':');
     time.append(calendar.get(java.util.Calendar.SECOND));
     String tt = time.toString();
     return tt;
}

Solution

  • Calendar calendar = Calendar.getInstance();
    Date myDate = new Date(); // May be your date too.
    calendar.setTime(myDate);
    

    I think this will work.