Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to format Time not Date on Android or Java?

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?

like image 1000
Xubing in China. Developer Avatar asked Nov 27 '22 12:11

Xubing in China. Developer


2 Answers

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

like image 169
Paola G Avatar answered Dec 04 '22 13:12

Paola G


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

like image 41
jlopes Avatar answered Dec 04 '22 13:12

jlopes