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:
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
Use the SimpleDateFormater
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String time = sdf.format(date);
SimpleDateFormat outFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(milliseconds);
String dateString = outFmt.format(date);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.