简体   繁体   English

在java中获取今天的日期

[英]get today date in java

This is my database: 这是我的数据库:

date          status
1343469690     Q
1343470540     C
1343470556     P

I have written this code: 我写了这段代码:

public class TodayQ {

    public int data() {
        int count = 0;
        //count++;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pro", "root", "");

            PreparedStatement statement = con.
                    prepareStatement("select * from orders where status='Q' AND date=CURDATE()");
            ResultSet result = statement.executeQuery();
            while (result.next()) {
                // Do something with the row returned.
                count++; //if the first col is a count.
            }
        } catch (Exception exc) {
            System.out.println(exc.getMessage());
        }
        return count;
    }
}

It works successfully when date is in yyyy-mm-dd format. 当日期为yyyy-mm-dd格式时,它可以成功工作。 But now my date is a timestamp in 1343469690 format. 但是现在我的日期是1343469690格式的时间戳。 How is get the count value? 如何获得计数值?

If it's actually a Timestamp, then you have the date: See http://docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html . 如果它实际上是时间戳记,则您具有日期:请参阅http://docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html Converting a TimeStamp to a Calendar (http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html) is trivial: see the Calendar.setTimeInMillis() method. 将TimeStamp转换为日历(http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html)很简单:请参见Calendar.setTimeInMillis()方法。

Replace CURDATE() into FROM_UNIXTIME() .According to your code, you should change likes this; CURDATE()替换为FROM_UNIXTIME() 。根据您的代码,您应该像这样进行更改;

      PreparedStatement statement = con.prepareStatement("select * from orders where status='Q' AND date=FROM_UNIXTIME(date,'%Y %M %D')");

See reference here . 请参阅此处的参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM