0

YouTube JSON-C GDATA フィードを解析していますが、JSON であるためコロン (:) を含めることはできません。そのため、文字列の長さを解析すると、823 または 2421 などのプレーン テキストとして表示されます。これをより読みやすくするにはどうすればよいですか? つまり、823 --> 8:23 または 2421 --> 24.21 または 23 --> 0:23?

4

3 に答える 3

1

あなたの質問に厳密に答えたいのですが、答えは次のとおりです。

//Check the length of the string and maybe add zero at the beginning
string = StringBuffer(string).insert(2, ":").toString();

注: string.getLength()-2 を使用する必要がありましたが、要点を理解していただければ幸いです ;-)

しかし、個人的には、時間を秒単位または別の形式で送信します。

String dateStr = "03/08/2010"; 

SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy"); 
Date dateObj = curFormater.parse(dateStr); 
SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy"); 

String newDateStr = postFormater.format(dateObj); 
于 2012-09-04T21:09:58.903 に答える
1

このコードはそれを行う必要があります:

String time = "322";
int length = time.length();
StringBuffer s = new StringBuffer(time);

switch(length) {
    case(1):    s.insert(0, "0:0"); break; //ex: 2 -> 0:02
    case(2):    s.insert(0, "0:"); break; //ex: 22 -> 0.22
    case(3):    s.insert(1, ":"); break;  //ex: 322 -> 3:22
    case(4):    s.insert(2, ":"); break; //ex: 2421 -> 24:21
}

case(5)また、時間の長さが予想される場合は、引き続き追加できます。

幸運を!

于 2012-09-05T00:51:36.330 に答える
0

http://www.exampledepot.com/egs/java.text/parsedate.html

それは役立つはずです;)

于 2012-09-04T21:04:54.897 に答える