Java 不带日期的排序时间
我正在尝试解决这个问题:下面是我的代码和输出: 代码Java 不带日期的排序时间,java,sorting,date,time,Java,Sorting,Date,Time,我正在尝试解决这个问题:下面是我的代码和输出: 代码 public class TimeSort implements Comparable<TimeSort>{ public int hour; public int minutes; public int seconds; public TimeSort(int hour, int minutes, int seconds) { this.hour = hour; this.min
public class TimeSort implements Comparable<TimeSort>{
public int hour;
public int minutes;
public int seconds;
public TimeSort(int hour, int minutes, int seconds) {
this.hour = hour;
this.minutes = minutes;
this.seconds = seconds;
}
public TimeSort(String str){
/*DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
final String x = str;
try {
Date time = sdf.parse(x);
//Time time = new Time(new SimpleDateFormat("HH:mm:ss").parse(x).getTime());
this.hour = time.getHours();
this.minutes = time.getMinutes();
this.seconds = time.getSeconds();
} catch (ParseException e) {
e.printStackTrace();
}*/
String[] parts = str.split(":");
int hour = Integer.parseInt(parts[0]);
int minutes = Integer.parseInt(parts[1]);
int seconds = Integer.parseInt(parts[2]);
this.hour = hour;
this.minutes = minutes;
this.seconds = seconds;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getMinutes() {
return minutes;
}
public void setMinutes(int minutes) {
this.minutes = minutes;
}
public int getSeconds() {
return seconds;
}
public void setSeconds(int seconds) {
this.seconds = seconds;
}
@Override
public String toString() {
if(this.getHour() < 10){
return "0"+this.getHour()+":"+this.getSeconds()+":"+this.getSeconds();
}
return this.getHour()+":"+this.getSeconds()+":"+this.getSeconds();
}
public static void main(String[] args){
List<String> inputs = new ArrayList<>();
inputs.add("02:26:31 14:44:45 09:53:27");
inputs.add("05:33:44 21:25:41");
inputs.add("02:26:31 14:44:45 09:53:27 02:26:31 01:44:45 19:53:27");
for (String input : inputs){
sortTimes(input);
}
我看到的奇怪的事情是《泰晤士报》没有正确印刷。例如,02:26:31打印为02:31:31。即使我试图分析字符串时间(代码的注释部分),也是一样的
感谢您的帮助。谢谢您的toString()
方法有一个bug:
@Override
public String toString() {
if(this.getHour() < 10){
return "0"+this.getHour()+":"+this.getSeconds()+":"+this.getSeconds();
}
return this.getHour()+":"+this.getSeconds()+":"+this.getSeconds();
}
@覆盖
公共字符串toString(){
if(this.getHour()<10){
返回“0”+this.getHour()+”:“+this.getSeconds()+”:“+this.getSeconds()”;
}
返回this.getHour()+“:”+this.getSeconds()+“:”+this.getSeconds();
}
请注意,第二个字段是getSeconds()
,而不是getMinutes()
您的toString()
方法有一个错误:
@Override
public String toString() {
if(this.getHour() < 10){
return "0"+this.getHour()+":"+this.getSeconds()+":"+this.getSeconds();
}
return this.getHour()+":"+this.getSeconds()+":"+this.getSeconds();
}
@覆盖
公共字符串toString(){
if(this.getHour()<10){
返回“0”+this.getHour()+”:“+this.getSeconds()+”:“+this.getSeconds()”;
}
返回this.getHour()+“:”+this.getSeconds()+“:”+this.getSeconds();
}
请注意,第二个字段是
getSeconds()
,而不是getMinutes()
您的toString()
-方法不会打印分钟 您的toString()
-方法不会打印分钟数 其他答案发现了您代码中的错误
java.time
仅供参考,所有这些代码都是不必要的。Java8及更高版本内置了该框架。在其类中,只用于表示一天中的某个时间值,不带日期和时区。此类已经知道如何对其值进行排序和排序,以及如何对其值进行排序。默认情况下,打印遵循标准格式
List<LocalTime> times = new ArrayList<>();
times.add( LocalTime.parse( "02:26:31" );
times.add( LocalTime.parse( "14:44:45" );
times.add( LocalTime.parse( "09:53:27" );
Collections.sort( times );
其他答案在您的代码中找到了错误 java.time 仅供参考,所有这些代码都是不必要的。Java8及更高版本内置了该框架。在其类中,只用于表示一天中的某个时间值,不带日期和时区。此类已经知道如何对其值进行排序和排序,以及如何对其值进行排序。默认情况下,打印遵循标准格式
List<LocalTime> times = new ArrayList<>();
times.add( LocalTime.parse( "02:26:31" );
times.add( LocalTime.parse( "14:44:45" );
times.add( LocalTime.parse( "09:53:27" );
Collections.sort( times );
您正在手工解析日期。这是如此容易出错,以至于我们不再深入研究代码。使用库代码进行解析,最好是从。@BoristheSpider看起来这是一个编码挑战,因此可能不鼓励使用库。@mdnghtblue我还没有遇到一个编码挑战,因为标准JDK功能的使用是不允许的。但如果是这样的话,OP需要在问题中明确这一点。你的问题是手工解析日期。这是如此容易出错,以至于我们不再深入研究代码。使用库代码进行解析,最好是从。@BoristheSpider看起来这是一个编码挑战,因此可能不鼓励使用库。@mdnghtblue我还没有遇到一个编码挑战,因为标准JDK功能的使用是不允许的。但如果是这样的话,OP需要在问题中明确这一点。是的,这就是问题所在。谢谢。给了你一票!你更快;-)+是的,这就是问题所在。谢谢你。给了你一票!你更快;-)+1.
System.out.println( times );