Java 无法比较时间范围

Java 无法比较时间范围,java,time,jodatime,date-comparison,Java,Time,Jodatime,Date Comparison,我有两个String变量: time22将存储Linux命令生成的时间 timeInTheList将存储我的ArrayList time22将获取最新时间,而timeInTheList将存储一些以前的时间 我想比较这两次是否已经过了30分钟,但我无法做到 我发现一些奇怪的数据打印出来的日期,时间和年份,我只需要时间,我不需要任何日期或年份 代码: 在Joda Time中,您可以使用DateTimeFormatter解析输入String并创建LocalTime对象-LocalTime似乎是最好

我有两个
String
变量:

  • time22
    将存储Linux命令生成的时间
  • timeInTheList
    将存储我的
    ArrayList
time22
将获取最新时间,而
timeInTheList
将存储一些以前的时间

我想比较这两次是否已经过了30分钟,但我无法做到

我发现一些奇怪的数据打印出来的日期,时间和年份,我只需要时间,我不需要任何日期或年份

代码:


在Joda Time中,您可以使用
DateTimeFormatter
解析输入
String
并创建
LocalTime
对象-
LocalTime
似乎是最好的选择,因为您只处理时间(小时/分钟/秒),不需要知道日期(天/月/年)

使用
LocalTime
对象,可以使用
Minutes
类获取它们之间的分钟数:

import org.joda.time.LocalTime;
import org.joda.time.Minutes;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

String time22 = "22:02:50";
String timeInTheList = "21:19:46";

// create formatter with format hour:minute:second
DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm:ss");

// parse Strings
LocalTime sTime2 = fmt.parseLocalTime(time22);
LocalTime fTime1 = fmt.parseLocalTime(timeInTheList);

// get the minutes between them
int minutes = Minutes.minutesBetween(fTime1, sTime2).getMinutes();
System.out.println(minutes); // 43
上述代码将输出
43
21:19:46
22:02:50
之间的分钟数)

由于输入位于(
HH:mm:ss
)中,您也可以在不使用格式化程序的情况下解析它们:

LocalTime sTime2 = LocalTime.parse(time22);
LocalTime fTime1 = LocalTime.parse(timeInTheList);

Java新的日期/时间API Joda Time正在被新的API取代,因此我不建议使用Joda启动新项目。甚至在文章中也提到:“请注意,Joda Time被认为是一个基本上“完成”的项目。没有计划进行重大的增强。如果使用Java SE 8,请迁移到Java.Time(JSR-310)。”

因此,如果您在Joda时间内没有一个庞大的代码库(这需要大量的工作才能迁移),或者如果您想使用新的API,请在下面检查如何使用它

旧类(
Date
Calendar
SimpleDateFormat
)已经存在,并且正在被新的API取代

如果使用<强> java 8 < />,请考虑使用这更容易


如果您使用的是Java如果您使用的是java8,则可以使用新的日期/时间API。 请尝试以下代码:

// Required imports
import java.time.LocalTime;
import java.time.Duration;

// Taken hard-coded values for testing purpose
String time22 = "22:02:50", timeInTheList = "21:19:46";

LocalTime Stime2 = LocalTime.parse(time22);
LocalTime Ftime1 = LocalTime.parse(timeInTheList);

System.out.println("Stime2: " + Stime2);
System.out.println("Ftime1: " + Ftime1);

Duration duration = Duration.between(Ftime1, Stime2);
long diff1 = duration.getSeconds();
System.out.println("duration: " + diff1);

if(diff1 > 1800){
  System.out.println("it is more than 30 minute in the list");
  /**
   * Your remaining code goes here.
   */
}else{
  System.out.println("Keep in the list first, still not reach 30 minute");
}

您已使用jodatime标记对此问题进行了标记,但您的代码未使用它。您是否正在使用(或想要使用)Joda Time?我有Joda Time库,但我不确定如何在这种情况下使用它。我用它来做另一件事purpose@yumi将这些澄清作为对您的问题的编辑,而不是评论。它按预期工作,为您加油,让我学习新东西Java 8方法工作,但joda time与其他库存在一些兼容问题,因为我使用Java 8,所以我只使用Java 8method@yumi好!!使用新的API更好!快乐编码!您还可以使用
duration.toMinutes()
而不是
getSeconds()
Yes,当然可以。但是在yumi在问题中发布的代码中,有一个秒的比较,所以我使用了getSeconds()方法。
LocalTime sTime2 = LocalTime.parse(time22);
LocalTime fTime1 = LocalTime.parse(timeInTheList);
// if you're using ThreeTen Backport, replace java.time by org.threeten.bp
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

String time22 = "22:02:50";
String timeInTheList = "21:19:46";

// parse Strings
LocalTime sTime2 = LocalTime.parse(time22);
LocalTime fTime1 = LocalTime.parse(timeInTheList);

// get the minutes between them
long minutes = ChronoUnit.MINUTES.between(fTime1, sTime2);
System.out.println(minutes); // 43
// Required imports
import java.time.LocalTime;
import java.time.Duration;

// Taken hard-coded values for testing purpose
String time22 = "22:02:50", timeInTheList = "21:19:46";

LocalTime Stime2 = LocalTime.parse(time22);
LocalTime Ftime1 = LocalTime.parse(timeInTheList);

System.out.println("Stime2: " + Stime2);
System.out.println("Ftime1: " + Ftime1);

Duration duration = Duration.between(Ftime1, Stime2);
long diff1 = duration.getSeconds();
System.out.println("duration: " + diff1);

if(diff1 > 1800){
  System.out.println("it is more than 30 minute in the list");
  /**
   * Your remaining code goes here.
   */
}else{
  System.out.println("Keep in the list first, still not reach 30 minute");
}