Reputation: 661
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?
Upvotes: 4
Views: 14039
Reputation: 798
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
Upvotes: 22
Reputation: 3006
SimpleDateFormat outFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(milliseconds);
String dateString = outFmt.format(date);
Upvotes: 0
Reputation: 1012
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
Upvotes: 5
Reputation: 2003
Use the SimpleDateFormater
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String time = sdf.format(date);
Upvotes: 2