我有白天,几小时,几小时。我想在这些变量中得到minimum
值而不是zero
。我怎么能得到它?如果这些变量是整数,我可以得到最小值。
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 :(得分: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");
}