我想知道什么是最好的方法是将00:00:00格式的时间字符串转换为整数或长整型? 我的最终目标是能够将一串字符串时间转换为整数/长整型,将它们添加到数组中,并在数组中找到最近的时间......
我会很感激任何帮助,谢谢!
好的,根据答案,我决定继续直接比较字符串。 但是,我有一些麻烦。 可能有多个“最近的”时间,也就是说,如果两次相等。 如果是这种情况,我想将这两个时间的索引添加到ArrayList中。 这是我现在的代码:
days[0] = "15:00:00"; days[1] = "17:00:00"; days[2] = "18:00:00"; days[3] = "19:00:00"; days[4] = "19:00:00"; days[5] = "15:00:00"; days[6] = "13:00:00"; ArrayList<Integer> indexes = new ArrayList<Integer>(); String curMax = days[0]; for (int x = 1; x < days.length1; x++) { if (days[x].compareTo(curMax) > 0) { curMax = days[x]; indexes.add(x); System.out.println("INDEX OF THE LARGEST VALUE: " + x); } }但是,这是将索引1,2和3添加到ArrayList ...
谁能帮我?
I'm wondering what the best method is to convert a time string in the format of 00:00:00 to an integer or a long? My ultimate goal is to be able to convert a bunch of string times to integers/longs, add them to an array, and find the most recent time in the array...
I'd appreciate any help, thanks!
Ok, based on the answers, I have decided to go ahead and compare the strings directly. However, I am having some trouble. It is possible to have more than one "most recent" time, that is, if two times are equal. If that is the case, I want to add the index of both of those times to an ArrayList. Here is my current code:
days[0] = "15:00:00"; days[1] = "17:00:00"; days[2] = "18:00:00"; days[3] = "19:00:00"; days[4] = "19:00:00"; days[5] = "15:00:00"; days[6] = "13:00:00"; ArrayList<Integer> indexes = new ArrayList<Integer>(); String curMax = days[0]; for (int x = 1; x < days.length1; x++) { if (days[x].compareTo(curMax) > 0) { curMax = days[x]; indexes.add(x); System.out.println("INDEX OF THE LARGEST VALUE: " + x); } }However, this is adding index 1, 2, and 3 to the ArrayList...
Can anyone help me?
最满意答案
你可以通过比较字符串找到最近的时间,你不需要先转换成一个长的字符串。
例如
String[] times = { "12:23:45", "23:59:59", "00:00:00" } Arrays.sort(times); String firstTime = times[0]; String lastTime = times[times.length-1];You can find the most recent time by comparing the strings, you don't need to convert to a long first.
e.g.
String[] times = { "12:23:45", "23:59:59", "00:00:00" } Arrays.sort(times); String firstTime = times[0]; String lastTime = times[times.length-1];更多推荐
发布评论