Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

stringtokenizer - Convert time value to format “hh:mm Am/Pm” using Android

I am getting date value from database like "2013-02-27 06:06:30" using StringTokenizer I will get time separately like below

String startTime = "2013-02-27 06:06:30";

StringTokenizer token = new StringTokenizer(startTime);
String date1 = token.nextToken();  
String time1 = token.nextToken(); 

and in time1 I am getting the result 06:06:30,

Can I re-store it in another variable of type String as follows?

String displayValue = "06:06 AM";

And if time1 variable has the value of

String time = 16:00:00;

then it should be converted to:

String displayValue = "04:00 PM";
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this..

Date dt = new Date(date1);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
String time1 = sdf.format(dt);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...