Java 如何比较Time.sql
我想将类中的Java 如何比较Time.sql,java,time,Java,Time,我想将类中的Time实例与当前系统时间进行比较。我正在使用before和after方法,但它会产生意外的结果 Time currentSystemTime = new Time(new java.util.Date().getTime()); Time t = Time.valueOf("23:25:00"); System.out.println(currentSystemTime+" currentTime"); System.out.println(t+" hardcoded Time
Time
实例与当前系统时间进行比较。我正在使用before
和after
方法,但它会产生意外的结果
Time currentSystemTime = new Time(new java.util.Date().getTime());
Time t = Time.valueOf("23:25:00");
System.out.println(currentSystemTime+" currentTime");
System.out.println(t+" hardcoded Time");
输出
21:24:48 currentTime
23:25:00 hardcoded Time
但是现在,如何比较这两次。我正在尝试以下方法。它不起作用
if(currentSystemTime.before(t))
{
System.out.println("Hello");
}
这个hello应该执行,但不是。有没有办法比较时间。可以通过调用getTime()以毫秒为单位获取时间。
现在比较一下,这样你就可以得到之前或之后的结果。这是因为他们在不同的年份
public static void main(String[] args) {
Time currentSystemTime = new Time(new java.util.Date().getTime());
Time t = Time.valueOf("23:25:00");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
System.out.println(currentSystemTime + " currentTime");
System.out.println(t + " hardcoded Time");
System.out.println(sdf.format(currentSystemTime));
System.out.println(sdf.format(t));
if (currentSystemTime.before(t)) {
System.out.println("Hello");
}
}
结果
17:04:13 currentTime
23:25:00 hardcoded Time
2013-10-15T17:04:13.758+0100
1970-01-01T23:25:00.000+0100
getTime()可以像@barwinkk提到的那样工作的原因是它返回自1970年1月1日00:00:00 GMT以来的毫秒长表示形式,由这个日期对象表示。Time currentSystemTime=new Time(new java.util.Date().getTime());
Time currentSystemTime = new Time(new java.util.Date().getTime());
Time t = Time.valueOf("23:25:00");
System.out.println(currentSystemTime+" currentTime");
System.out.println(t+" hardcoded Time");
if(currentSystemTime.getTime()<t.getTime())
{
System.out.println("Hello");
}
时间t=时间的值(“23:25:00”);
System.out.println(currentSystemTime+“currentTime”);
系统输出打印项次(t+“硬编码时间”);
如果(currentSystemTime.getTime()更好的是time.getTime(),我正在证明OP为什么认为他得到了错误的结果。你关于年份的论点是无效的--time
object允许你比较date
@barwnik中忽略日期部分的时间,我认为你没有领会david演示的要点