我正在尝试解决以下问题:https://www.codeeval.com/browse/214/下面是我的代码和输出: 代码
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);
}
@Override
public int compareTo(TimeSort timeSort) {
if(this.getHour() > timeSort.getHour()){
return -1;
}
else if(this.getHour() < timeSort.getHour()){
return 1;
}
else if(this.getHour() == timeSort.getHour()) {
if(this.getMinutes() > timeSort.getMinutes()){
return -1;
}
else if(this.getMinutes() < timeSort.getMinutes()){
return 1;
}
}
else if(this.getSeconds() > timeSort.getSeconds()){
return 1;
}
return 0;
}
public static void sortTimes(String str){
List<TimeSort> list = new ArrayList<>();
String[] times = str.split(" ");
for (String time : times){
list.add(new TimeSort(time));
}
System.out.print("Before Sorting: ");
for (TimeSort t : list){
System.out.print(t + " ");
}
System.out.println();
Collections.sort(list);
System.out.print("After Sorting: ");
for (TimeSort t : list){
System.out.print(t + " ");
}
System.out.println();
System.out.println("==============================================================");
}
}
输出
Before Sorting: 02:31:31 14:45:45 09:27:27
After Sorting: 14:45:45 09:27:27 02:31:31
==============================================================
Before Sorting: 05:44:44 21:41:41
After Sorting: 21:41:41 05:44:44
==============================================================
Before Sorting: 02:31:31 14:45:45 09:27:27 02:31:31 01:45:45 19:27:27
After Sorting: 19:27:27 14:45:45 09:27:27 02:31:31 02:31:31 01:45:45
==============================================================
我看到的奇怪事情是时间打印不正确。例如 02:26:31 打印为 02:31:31。即使我试图解析字符串时间也是一样的(代码的注释部分)
感谢任何帮助。谢谢
最佳答案
您的 toString()
方法有一个错误:
@Override
public String toString() {
if(this.getHour() < 10){
return "0"+this.getHour()+":"+this.getSeconds()+":"+this.getSeconds();
}
return this.getHour()+":"+this.getSeconds()+":"+this.getSeconds();
}
请注意,第二个字段是 getSeconds()
而不是 getMinutes()
。
关于java - 没有日期的排序时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32784814/