Java converts time format to integer or long

I am wondering what is the best method to convert a time string in the format 00:00:00 to integer or long? My ultimate goal is to be able to convert a bunch of time strings to integers / longs, add them to the array and find the most recent time in the array ...

I would be grateful for any help, thanks!


Well, based on the answers, I decided to go and compare the strings directly. However, I have a problem. It is possible to have more than one "last" time, that is, if two times are equal. If so, I want to add the index of both of these 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 adds indexes 1, 2, and 3 to the ArrayList ...

Can anybody help me?

+3
6

, , .

.

 String[] times = { "12:23:45", "23:59:59", "00:00:00" }
 Arrays.sort(times);
 String firstTime = times[0];
 String lastTime = times[times.length-1];
+5

java.text.SimpleDateFormat. parse(), . .

+4

simpleDateFormat getTime() , .

0

.

, , . , 8- , String.compareTo() .

( ), Integer.parseInt(s.substring(0,2) + s.substring(3, 5) + s.substring(6)).

0

Adding to @Bozho's answer: if you have a Date object from SimpleDateFormat, you call Date.getTime () to get the time as the long time you are looking for.

0
source

Well, as in other sentences here, you can use Collections.sort(ArrayList)where Arraylist contains all date objects. If you only need the latter, select the last value from this ArrayList. I feel this should do for you.

0
source

Source: https://habr.com/ru/post/1781858/


All Articles