2 つの 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 を取得します。