2014-01-14 92 views
0

我有一天,幾秒,幾小時。我想獲得minimum值,但不包括zero其中的變量。我怎麼弄到的?如果這些變量是整數,我可以得到最小值。得到最小值但大於0

long time = 1387670712 * (long) 1000; 
    Date past = new Date(time); 
    Date now = new Date(); 

    String day = TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime()) + " days ago"; 
    String sec = TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime()) + "minutes ago"; 
    String hours = TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime()) + " hours ago"; 
+0

排序,並得到最小的一個! – Rugal

+0

或只是比較他們....得到最小的一個! – Rugal

+0

value +「String」。我如何分類? –

回答

0

看來你需要像下一個:

public static void main(String args[]) { 
    long time = 1387670712 * (long) 1000; 
    Date past = new Date(time); 
    Date now = new Date(); 

    long i1 = TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime()); 
    long i2 = TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime()); 
    long i3 = TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime()); 
    String day = i1+" days ago"; 
    String sec = i2+" minutes ago"; 
    String hours = i3 +" hours ago"; 
    long[] values = new long[] {i1,i2,i3}; 
    Arrays.sort(values); 
    for(int i = 0 ;i<values.length;i++){ 
     if(values[i] != 0){ 
      String postfix = values[i] == i1 ? day : (values[i] == i2 ? sec : hours); 
      System.out.println("minimum = " + postfix); 
      System.exit(0); 
     } 
    } 
    System.out.println("no values or all 0"); 
} 
+0

謝謝。你能否描述這一行String postfix = values [i] == i1? day:(values [i] == i2?sec:hours); –

+0

這是三元運算符(if-then-else的替代方法)。 – alex2410

相關問題