今天在java中获取日期

时间:2012-08-06 10:43:37

标签: java date timestamp

这是我的数据库:

date          status
1343469690     Q
1343470540     C
1343470556     P

我写了这段代码:

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;
    }
}

当日期为yyyy-mm-dd格式时,它可以成功运行。但现在我的日期是1343469690格式的时间戳。如何获得计数值?

2 个答案:

答案 0 :(得分:0)

如果它实际上是时间戳,那么您有日期:请参阅http://docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html。将TimeStamp转换为日历(http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html)是微不足道的:请参阅Calendar.setTimeInMillis()方法。

答案 1 :(得分:0)

CURDATE()替换为 FROM_UNIXTIME()。根据您的代码,您应该更改喜欢此内容;

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

参见参考here