比较java中的两个时间戳

use*_*130 43 java timestamp

我如何比较是否mytimefromtime和之间totime:

Timestamp fromtime;
Timestamp totime;

Timestamp mytime;
Run Code Online (Sandbox Code Playgroud)

leg*_*ess 61

if(mytime.after(fromtime) && mytime.before(totime))
  //mytime is in between
Run Code Online (Sandbox Code Playgroud)


Jea*_*art 15

使用beforeafter方法:Javadoc

if (mytime.after(fromtime) && mytime.before(totime))
Run Code Online (Sandbox Code Playgroud)


Nic*_*kLH 9

来自:http://download.oracle.com/javase/6/docs/api/java/sql/Timestamp.html#compareTo(java.sql.Timestamp)

public int compareTo(Timestamp ts)
Run Code Online (Sandbox Code Playgroud)

将此Timestamp对象与给定的Timestamp对象进行比较.参数:ts - 要与此Timestamp对象进行比较的Timestamp对象返回:如果两个Timestamp对象相等,则返回值0;否则返回0.如果此Timestamp对象在给定参数之前,则小于0的值; 如果此Timestamp对象位于给定参数之后,则值大于0.自:1.4


Mau*_*rry 6

if (!mytime.before(fromtime) && !mytime.after(totime))
Run Code Online (Sandbox Code Playgroud)