Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 找出两次比较接近的时间_Java_Android - Fatal编程技术网

Java 找出两次比较接近的时间

Java 找出两次比较接近的时间,java,android,Java,Android,我想问一些关于Java的问题。我有从JSON数据解析的时间(字符串)列表。我需要找出哪个时间更接近系统时间。是这样的,现在时间是16:40。我的时间清单上有 “16:32”、“16:38”、“16:44”、“16:50”等等。对于16:40,16:38比16:44更接近,我需要找到这个。我试着获取当前索引和下一个索引,解析它们并初始化新的日历等等。但我不知道下一步该怎么做 这个问题有什么解决办法吗 String returnTime = current.getDonus();

我想问一些关于Java的问题。我有从JSON数据解析的时间(字符串)列表。我需要找出哪个时间更接近系统时间。是这样的,现在时间是16:40。我的时间清单上有 “16:32”、“16:38”、“16:44”、“16:50”等等。对于16:40,16:38比16:44更接近,我需要找到这个。我试着获取当前索引和下一个索引,解析它们并初始化新的日历等等。但我不知道下一步该怎么做

这个问题有什么解决办法吗

       String returnTime = current.getDonus();
        if (i < list.size() + 1) {
            TimeList nextOne=list.get(i+1);
            String nextReturnTime = nextOne.getDonus();
            String[] parsedNextReturn = nextReturnTime.split(":");
            String[] parsedReturn = returnTime.split(":");

            Date date = new Date();

            Calendar calNextReturn= Calendar.getInstance();
            calNextReturn.setTime(date);
            calNextReturn.set(Calendar.HOUR_OF_DAY, Integer.parseInt(parsedNextReturn[0]));
            calNextReturn.set(Calendar.MINUTE, Integer.parseInt(parsedNextReturn[1]));
            calNextReturn.set(Calendar.SECOND, 0);
            calNextReturn.set(Calendar.MILLISECOND, 0);

            Calendar calCurrentReturn= Calendar.getInstance();
            calCurrentReturn.setTime(date);
            calCurrentReturn.set(Calendar.HOUR_OF_DAY, Integer.parseInt(parsedReturn[0]));
            calCurrentReturn.set(Calendar.MINUTE, Integer.parseInt(parsedReturn[1]));
            calCurrentReturn.set(Calendar.SECOND, 0);
            calCurrentReturn.set(Calendar.MILLISECOND, 0);

            Calendar calSystem = Calendar.getInstance();
            calSystem.setTime(date);
            calSystem.set(Calendar.SECOND, 0);
            calSystem.set(Calendar.MILLISECOND, 0);
        }
String returnTime=current.getDonus();
如果(i
试试这个:

        long smallestABS = Long.MAX_VALUE;
        long systemTime = System.currentTimeMillis();
        long timeClosest;
        for (long time : timeList) {
            long abs = Math.abs(systemTime - time);
            if(smallestABS > abs){
                smallestABS = abs;                
                timeClosest = time;
            }
        }

查找
abs的最小值(givenTime.getTimeInMillis()-timeToCompare.getTimeInMillis())
但是对于Math.abs,我需要两个参数。我有3.5美元,嗯?mat.abs有一个参数…它只是
(i)->i>0?i:-i
givenTime
是您的系统时间,
timeToCompare
是来自json的时间之一。。。现在你需要迭代并找到minimumI,我用的是abs,没关系。但现在看来它的时间是上午/下午,而不是24小时。是否是上午/下午并不重要,你应该将时间设置为毫秒。您可以使用日历来执行此操作,在那里设置完整日期并使用calendar.getTimeInMillis()然后您可以将其与systemTime进行比较