我想将一个类中的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应该执行,但是没有执行。有没有办法比较时间。
最佳答案
因为他们在不同的年份,
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 以来此 Date 对象表示的毫秒长表示。
关于java - 如何比较Time.sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19385794/