gpt4 book ai didi

Java 和 SQL 比较日期

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:14:08 27 4
gpt4 key购买 nike

我正在尝试获取客户到达日期并将其与我的 SQL 数据库进行比较,以查看我的数据库中是否存在相同的日期。但是我收到以下错误:The operator > is undefined for the argument type(s) java.lang.String, java.lang.String

P.S 我需要通过 java 比较它而不是使用 sql 查询

public void makeNewReservation() throws ParseException {
// Enter informations

System.out.println("Date of arrivel?");
Scanner in = new Scanner(System.in);
String date_entree = in.next();
System.out.println("Date of exit? dd/MM/yyyy");
String date_sortiee = in.next();
calculateDaysDifference(date_sortiee, date_entree);



public void calculateDaysDifference(String date_entree, String date_sortiee) throws ParseException{


ConnectionMySQL myConnection=new ConnectionMySQL();
Connection conSQL=myConnection.startDBConnection();
boolean connectionOK=myConnection.checkConnection(conSQL);

String query = ("SELECT `START_DATE`,`END_DATE` FROM `room_booking");


//if everything is fine with the connection, i try to execute a query
if (connectionOK){
try{
ResultSet mesResultats=myConnection.executeQuery(conSQL, query);

//the while loop is just for me to check the dates
while (mesResultats.next()) {
System.out.println("START_DATE: "+mesResultats.getString(1)+" END_DATE : "+ mesResultats.getString(2));


if (date_entree > mesResultats.getString(1){
System.out.println("cant reserve room room reserved already");


}
}


// je ferme la connexion
conSQL.close();

}

catch(SQLException e){
e.printStackTrace();


}

}

my data base

最佳答案

你需要比较2个日期

1) 将输入的字符串转换为日期

SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
java.util.Date d=df.format(/*date String*/);

NOTE: df.format will throw parseException if the String format does not match "yyyy-MM-dd" . I leave it upto you to make sure the date string is of the specified format.

2)从sql查询获取日期

java.util.Date sqlDate=new java.util.Date(resultset.getDate().getTime());

NOTE : resultset.getDate() will give you java.sql.Date class's object.

3) Compare 2 dates

关于Java 和 SQL 比较日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14757836/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com