计算时间戳之间的年,月,日差异

时间:2012-10-15 09:12:38

标签: java sql timestamp

  

可能重复:
  How to calculate the difference between two Java java.sql.Timestamps?

如何计算从Java中使用SQL获取的两个时间戳之间的年/月/日(实际上不需要时间)?

以下是我正在使用的代码片段:

query = "SELECT bc.periodbeginningdate " +
            "FROM cus_billingcontract bc " +
            "WHERE cus_billingcontract_id = " + recordID;
Timestamp bDate = DB.getSQLValueTS(null, query);

query = "SELECT bc.periodenddate " +
            "FROM cus_billingcontract bc " +
            "WHERE cus_billingcontract_id = " + recordID;
Timestamp eDate = DB.getSQLValueTS(null, query);

1 个答案:

答案 0 :(得分:1)

您可以使用long将时间戳转换为getTime():请点击此处查看大量信息:

How to calculate the difference between two Java java.sql.Timestamps?

然后,使用long值,您可以使用JodaTime库轻松访问月差异。

见这里:

How to calculate difference ONLY in months using Java's Joda API