-2

如何在java中获取当前日期详细信息?我开发了一个类:

 public class RetailerWs {

 public String customerData(){
  String customerInfo = "";
  try{
  Class.forName("com.mysql.jdbc.Driver");
  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pro","root","");
   //Find customer information where the customer ID is maximum
   PreparedStatement statement =  con.prepareStatement("select * from orders where status='Q' AND date='2012-08-01'");
   ResultSet result = statement.executeQuery();
   int count=0;
    count++;
  // System.out.println(count);
   while(result.next()){
  System.out.println(count);
  }
      }
 catch(Exception exc){
 System.out.println(exc.getMessage());
  }

 return customerInfo;
 }

}

这是另一堂课。

  public class Demo {
  public static void main(String[] args){
    RetailerWs obj = new RetailerWs();
    System.out.println(obj.customerData());
  }

 }

如果我运行demo,它会在之后调用RetailerWs它显示的值count。问题是目前我手动插入日期,即

   PreparedStatement statement =  con.prepareStatement("select * from orders where status='Q' AND date='2012-08-01'");

但我想要一个获取当前日期的查询。如何创建和调用它?请帮我。

4

4 回答 4

4

你可以试试

select * from orders where status='Q' AND date = CURDATE();
于 2012-08-01T09:35:39.023 回答
1

您可以使用日历实现它,如:

public static Date getToday(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    return c.getTime();
}
于 2012-08-01T09:36:34.860 回答
1

用问号替换您准备好的语句中的日期,并使用该setDate()方法设置其值:

statement = con.prepareStatement("select * from orders where status='Q' AND date=?");
statement.setDate(1, new Date());

的第一个参数setDate()是 的索引(从 1 开始)?。将此机制也用于其他参数 (Q) 将是一种很好的做法。

于 2012-08-01T09:41:39.163 回答
0

如果日期列包含时间部分,您需要修改如下查询

select * from orders where status='Q' AND 
date >= CURDATE() and date<CURDATE() +interval 1 day ; 
于 2012-08-01T09:57:56.633 回答