日期:2014-05-20 浏览次数:20866 次
public class Test6 { public static void main(String[] args) { String[] arrStr1={"00000004", "暂无姓名"}; String[] arrStr4={"00000004", "暂无姓名"}; String[] arrStr2={"2011-10-10", "08:08:59"}; String[] arrStr3={"2011-10-10", "18:08:59"}; Map<String[], String[]> map=new HashMap<String[], String[]>(); map.put(arrStr1, arrStr2); map.put(arrStr4, arrStr3); Iterator it=map.keySet().iterator(); while (it.hasNext()) { String[] key=(String[]) it.next(); String[] value=(String[]) map.get(key); System.out.println(Arrays.toString(key)+" "+Arrays.toString(value)); } } } 如何在key一样的情况下。并且value的第零个元素 也就是日期相等的情况下 计算时间差
import java.util.*; public class Test6 { public static void main(String[] args) { String[] arrStr1={"00000004", "暂无姓名"}; String[] arrStr4={"00000004", "暂无姓名"}; String[] arrStr2={"2011-10-10", "08:08:59"}; String[] arrStr3={"2011-10-10", "18:08:59"}; Map<String[], String[]> map=new HashMap<String[], String[]>(); map.put(arrStr1, arrStr2); map.put(arrStr4, arrStr3); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); List<String[][]> list = new ArrayList<String[][]>(); for (Map.Entry<String[], String[]> entry : map.entrySet()) { list.add(new String[][]{entry.getKey(), entry.getValue()}); } for (int i=0; i<list.size(); i++) { String[] key1 = list.get(i)[0]; String[] value1 = list.get(i)[1]; for (int j=i+1; j<list.size(); j++) { String[] key2 = list.get(j)[0]; String[] value2 = list.get(j)[1]; if (Arrays.equals(key1, key2) && value1[0].equals(value2[0])) { Date d1 = sdf.parse(value1[0] + " " + value1[1]); Date d2 = sdf.parse(value2[0] + " " + value2[1]); long dif = Math.abs(d1.getTime() - d2.getTime()); System.out.printf("key1=%s, value1=%s\n", Arrays.toString(key1), Arrays.toString(value1)); System.out.printf("key2=%s, value2=%s\n", Arrays.toString(key2), Arrays.toString(value2)); System.out.printf("hour:%d, minute:%d, second:%d\n", (dif/1000)/3600, ((dif/1000)%3600)/60, (dif/1000)%60); } } } } }