Java将时间格式转换为整数或长整型(Java convert time format to integer or long)

编程入门 行业动态 更新时间:2024-10-15 04:21:28
Java将时间格式转换为整数或长整型(Java convert time format to integer or long)

我想知道什么是最好的方法是将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];

更多推荐

本文发布于:2023-08-06 06:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446616.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   转换为   整型   格式   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!