0

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;
}
default locale
  • 13,035
  • 13
  • 56
  • 62
PHPFan
  • 756
  • 3
  • 12
  • 46
  • I'm not sure that's going to work, but what about using: `calendar.setTimeInMillis(System.currentTimeMillis());` after `getInstance`? – default locale Mar 06 '13 at 11:18

2 Answers2

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

I think this will work.

Rahul
  • 44,383
  • 11
  • 84
  • 103
0

R.j thankfully has solved my problem.

So the complete code will be

public String   Refresh()
    {

    calendar = calendar.getInstance();
    Date myDate = new Date();  
calendar.setTime(myDate);
     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;


}
PHPFan
  • 756
  • 3
  • 12
  • 46