在查找两个 sql 日期之间的差异时,我得到了错误的天数。下面展示了该方法:
public Vector getAvailRoom(String RoomID)
{
formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date dateA = null;
Date dateD = null;
Vector vect = new Vector();
try
{
psmt=con.prepareStatement("select * from Booking where RmID="+RoomID);
rs=psmt.executeQuery();
while(rs.next())
{
AvailDetails2 avaDet = new AvailDetails2();
avaDet.BookArrivalDate = rs.getString("BookArrivalDate");
avaDet.BookDepartureDate = rs.getString("BookDepartureDate");
System.out.println(""+avaDet.BookArrivalDate);
System.out.println(""+avaDet.BookDepartureDate);
try
{
dateA = (Date) formatter.parse(avaDet.BookArrivalDate);
dateD = (Date) formatter.parse(avaDet.BookDepartureDate);
DateTime datetimeA = new DateTime(dateA);
DateTime datetimeD = new DateTime(dateD);
diffDays = Days.daysBetween(datetimeA.toLocalDate(), datetimeD.toLocalDate()).getDays();
System.out.println(""+diffDays);
avaDet.BookDiffDays = Integer.toString(diffDays);
}
catch (Exception egetAvailRoom)
{
egetAvailRoom.printStackTrace();
}
vect.add(avaDet);
}
}
catch (Exception e3)
{
}
return vect;
}//end getAvailRoom
认为
BookArrivalDate
是2013-10-01 00:00:00
和
BookDepartureDate
是2013-10-14 00:00:00
我得到 4748 作为差异。