[英]How to perform arithmetic on java.sql.Time objects?
I have some java.sql.Time objects as follows: 我有一些java.sql.Time对象,如下所示:
String s1 = "12:00:04";
String s2 = "12:23:34";
String s3 = "12:03:02";
String s4 = "11:00:54";
int dij = j-i;
int dmj = j-m;
java.sql.Time tdi = java.sql.Time.valueOf(s1);
java.sql.Time tki = java.sql.Time.valueOf(s2);
java.sql.Time tdm = java.sql.Time.valueOf(s3);
java.sql.Time tdj = java.sql.Time.valueOf(s4);
How can I perform subtract operations of these objects? 如何执行这些对象的减法运算? I want to get the result in java.sql.Time object itself but number of seconds will work too.
我想在java.sql.Time对象本身中获取结果,但是秒数也可以工作。 Most of the methods given in java.sql.Time class are deprecated.
不推荐使用java.sql.Time类中给出的大多数方法。 http://docs.oracle.com/javase/7/docs/api/java/sql/Time.html
http://docs.oracle.com/javase/7/docs/api/java/sql/Time.html
Since java.sql.Time
wraps a java.util.Date
, I'm not sure that subtraction and putting the result in a java.sql.Time
object is meaningful . 由于
java.sql.Time
包装了java.util.Date
,因此我不确定将减法并将结果放入java.sql.Time
对象是否有意义 。
I would rather perform the subtraction (most likely using the milliseconds field and the getTime accessor), and store the result as a numeric representing the number of seconds/milliseconds etc. 我宁愿执行减法(最有可能使用毫秒字段和getTime访问器),并将结果存储为代表秒/毫秒等的数字。
If you're performing a lot of arithmetic, the solution is most likely to look at Joda-Time . 如果您要执行大量算术运算,则最有可能使用Joda-Time解决方案。 See the FAQ for arithmetic-related info.
请参阅常见问题解答以获取与算术相关的信息。 Joda-Time has a more consistent and useful API, plus is thread-safe (unlike the standard
java.util.Date/Calendar
) Joda-Time具有更一致和有用的API,并且具有线程安全性(与标准
java.util.Date/Calendar
不同)
I suggest you tu use the Joda Time java library which is very good at handling dates and times in Java. 我建议您使用Joda Time Java库 ,该库非常擅长处理Java中的日期和时间。
example: 例:
public boolean isRentalOverdue(DateTime datetimeRented) {
Period rentalPeriod = new Period().withDays(2).withHours(12);
return datetimeRented.plus(rentalPeriod).isBeforeNow();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.