I want to format the time like this: 2011-05-11 11:22:33
.
I had the following code:
Time time = new Time(MobiSageUtility.TIMEZONE_STRING);
time.setToNow();
String timeStr= time.format("yyyy-MM-dd HH:mm:ss");
However, that gives the date as: "yyyy-MM-dd HH:mm:ss"
not "2011-05-11 11:22:33"
.
After looking at the Android reference documents, I tried the following code:
String timeStr = time.format2445();
But that gives a string like: 20110511T112233
. Can somebody tell me how to format the time correctly?
String timeStr= time.format("%Y:%m:%d %H:%M:%S");
See man strftime as doc of method format explains:
http://linux.die.net/man/3/strftime
Use Date instead of Time and then use SimpleDateFormat.
Example:
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String dateString = fmt.format(date);
http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With