2012-10-18 245 views

回答

3

看看PrettyTime,它建立在JodaTime之上。

+3

+1但是,男人,只是試圖讓這個項目走出門外,不要把更多的精力放在我身上;-) – virtualeyes

14

雖然喬達並沒有直接實現這個,你不需要一個外部庫,只需求助於這樣一個簡單的實現。

/** 
* Returns the correct suffix for the last digit (1st, 2nd, .. , 13th, .. , 23rd) 
*/ 
public static String getLastDigitSufix(int number) { 
    switch((number<20) ? number : number%10) { 
     case 1 : return "st"; 
     case 2 : return "nd"; 
     case 3 : return "rd"; 
     default : return "th"; 
    } 
} 

響應於MHaris,上面的代碼產生

for (int i = 0; i < 99; i++) { 
    System.out.print(i + getLastDigitSufix(i) + ", "); 
} 

0th, 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 
10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 
20th, 21st, 22nd, 23rd, 24th, 25th, 26th, 27th, 28th, 29th, 
30th, 31st, 32nd, 33rd, 34th, 35th, 36th, 37th, 38th, 39th, 
40th, 41st, 42nd, 43rd, 44th, 45th, 46th, 47th, 48th, 49th, 
50th, 51st, 52nd, 53rd, 54th, 55th, 56th, 57th, 58th, 59th, 
60th, 61st, 62nd, 63rd, 64th, 65th, 66th, 67th, 68th, 69th, 
70th, 71st, 72nd, 73rd, 74th, 75th, 76th, 77th, 78th, 79th, 
80th, 81st, 82nd, 83rd, 84th, 85th, 86th, 87th, 88th, 89th, 
90th, 91st, 92nd, 93rd, 94th, 95th, 96th, 97th, 98th, 99th 

其中,根據English Ordinals in Wikipedia看起來是正確的。

+0

你也可能因爲簡單的實現錯誤而被低估 - 這段代碼產生了「11st 「,」第12「和」第13「,所有這些都應該有後綴」th「。 我想編輯帖子,但隊列顯然已滿。 – MHarris

+1

@MHarris很高興你沒有編輯。看起來不錯,請檢查一下自己! :) – Frankie

+0

哦,非常好!看起來很棒,我的不好。 :-) – MHarris

相關問題