在java中比较两次或多或少
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19046058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
comparing the two times is greater or lesser in java
提问by user2767354
Date date1= new java.util.Date();
java.sql.Date Sqldob = new java.sql.Date(date1.getTime());
System.out.println("date" +Sqldob);
Time Sqldob1 = new Time(date1.getTime());
System.out.println("User Time: " +Sqldob1);
String yourTime="09:30:00";
SimpleDateFormat ra = new SimpleDateFormat("HH:mm:ss");
Date yourDate = ra.parse(yourTime);
Time sqlTime3 = new Time(yourDate.getTime());
System.out.println("your time"+sqlTime3);
if(Sqldob1.before(sqlTime3)){
Sqldob1 = sqlTime3;
System.out.println("inside loop");
}
In the code above I am comparing two time variables for equality, but it is giving me the same value -1 for all the types of input
在上面的代码中,我比较了两个时间变量是否相等,但它为所有类型的输入提供了相同的值 -1
采纳答案by SudoRahul
You need to use the Date#before(Date),Date#after(Date)and Date#equals(Date)methods for basic date comparisons.
您需要使用Date#before(Date)、Date#after(Date)和Date#equals(Date)方法进行基本日期比较。
E.g:
例如:
Date d1 = new Date();
Date d2 = new Date();
if(d1.after(d2)){
// Do something
}
if(d1.before(d2)){
// Do something
}
if(d1.equals(d2)){
// Do something
}
You can use the Date#compareTo(Date)method also, but then, you need to interpret the output of the compareTo
method accordingly.
您也可以使用Date#compareTo(Date)方法,但是,您需要相应地解释该compareTo
方法的输出。
As the docs say:
正如文档所说:
The value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument.
如果参数 Date 等于此 Date,则值为 0;如果此日期在日期参数之前,则为小于 0 的值;如果此日期在日期参数之后,则该值大于 0。
In your case, you are getting -1
because
在你的情况下,你得到的-1
是因为
new SimpleDateFormat("HHH:mm:ss");
is wrong. Should be newSimpleDateFormat("HH:mm:ss");
int compare= sqlTime3.compareTo(Sqldob1);
This sqlTime3 has only time in it. The date is the epoch date as you've not mentioned that, and hence, its always going to be beforenew Date()
which is today.
new SimpleDateFormat("HHH:mm:ss");
是错的。应该是新的SimpleDateFormat("HH:mm:ss");
int compare= sqlTime3.compareTo(Sqldob1);
这个 sqlTime3 只有时间。日期是你没有提到的是,时代的日期,因此,它始终将是之前new Date()
这是今天。
Your solution:-(Hope this addresses your problem)
您的解决方案:-(希望这能解决您的问题)
java.util.Date date1= new java.util.Date();
Time Sqldob1 = new Time(date1.getTime());
System.out.println("User Time: " +Sqldob1);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 19); // Your hour
cal.set(Calendar.MINUTE, 30); // Your Mintue
cal.set(Calendar.SECOND, 00); // Your second
Time sqlTime3 = new Time(cal.getTime().getTime());
System.out.println("your time: "+sqlTime3);
if(Sqldob1.before(sqlTime3)){
Sqldob1 = sqlTime3;
System.out.println("inside loop");
}
回答by Lucky
You need to parse the date and call methods like before()
, after()
and equals()
like this,
您需要解析日期和呼叫方法,如before()
,after()
和equals()
这样的,
if(date.before(date1)){
System.out.println(" date is before date1 ");
}
if(date.after(date1)){
System.out.println(" date is after date1 ");
}
If both date
and date1
are equal you can use equals method,
如果这两个date
和date1
是相等的,你可以使用equals方法,
if(date.equals(date1)){
System.out.println(" date and date1 are equal");
}
回答by Dax Joshi
compareTo()
method always return zero for equal and non-zero for unequal dates.
compareTo()
方法对于相等的日期总是返回零,对于不相等的日期总是返回非零。
回答by Ole V.V.
Modern version:
现代版:
LocalDateTime dateTime1 = LocalDateTime.now(ZoneId.systemDefault());
LocalDate dob = dateTime1.toLocalDate();
System.out.println("date " + dob);
LocalTime dob1 = dateTime1.toLocalTime();
System.out.println("User Time: " + dob1);
String yourTime = "09:30:00";
LocalTime time3 = LocalTime.parse(yourTime);
System.out.println("your time " + time3);
if (dob1.isBefore(time3)) {
dob1 = time3;
System.out.println("inside if statement");
}
When I ran this code this morning, it printed:
今天早上我运行这段代码时,它打印出:
date 2017-07-07
User Time: 05:32:01.881
your time 09:30
inside if statement
The point is: With the old and now long outdated classes Date
and Time
it is easy notto get things right. With the modern classes I use here, it's much easier to getthem right.
重点是:使用旧的和现在已经过时的课程Date
,Time
很容易不把事情做好。使用我在这里使用的现代类,使它们正确更容易。
Are you using java.sql
types because you really need to get your date and time from a database and/or store them into one? This was what these were for, you shouldn't really have used them for other purposes. I use “was” and “were” intentionally because you don't need them for this purpose either anymore. With a new JDBC driver, you can get a LocalDateTime
from the database and store one back, or depending on your column datatype get an Instant
and convert it to LocalDateTime
:
您是否使用java.sql
类型是因为您确实需要从数据库中获取日期和时间和/或将它们存储为一个?这就是这些的用途,您不应该将它们用于其他目的。我故意使用“was”和“were”,因为您也不再需要它们用于此目的。使用新的 JDBC 驱动程序,您可以LocalDateTime
从数据库中获取一个并将其存储回来,或者根据您的列数据类型获取一个Instant
并将其转换为LocalDateTime
:
LocalDateTime dateTime2 = instantFromDb.atZone(ZoneId.systemDefault())
.toLocalDateTime();
PS Item 2. in SoduRahul's answergives the real and correct explanation of what went wrong in your program: though Time
was meant for time-of-day only, your Sqldob1
ends up holding today's date and sqlTime3
the date of the epoch (January 1, 1970), so the former will always be after the latter by their before
method.
PS 项目 2. 在SoduRahul 的回答中,对您的程序出了什么问题给出了真实且正确的解释:虽然Time
仅适用于一天中的时间,但您Sqldob1
最终持有今天的日期和sqlTime3
纪元的日期(1970 年 1 月 1 日) ,所以按照他们的before
方法,前者总是在后者之后。