【问题标题】:get today date in java在java中获取今天的日期
【发布时间】:2012-08-06 10:43:37
【问题描述】:

这是我的数据库:

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 格式的时间戳。如何获取计数值?

【问题讨论】:

  • “获取”日期的代码在哪里?您是否看到任何错误/异常?
  • 究竟是什么问题? 以什么方式不起作用?
  • 当日期为 yyyy-mm-dd 格式时,t 可以成功运行。但现在我的日期是 1343469690 格式的时间戳。如何获取dis时间戳格式的计数值?

标签: java date timestamp


【解决方案1】:

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

【讨论】:

  • 如何在我的代码中调用上述方法...我无法理解。请给我一些解决方案
【解决方案2】:

将 CURDATE() 替换为 FROM_UNIXTIME() 。根据您的代码,您应该像这样更改;

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

参见参考here。

【讨论】:

  • 控制台显示以下异常:语法错误或来自服务器的访问冲突消息:“调用本机函数'FROM_UNIXTIME'的参数计数不正确”0
  • 现在我只需要插入2个订单..然后它必须显示计数值为2.但显示的dcount值为0
  • hi显示计数值为0而已。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-28
  • 2014-09-13
  • 2019-05-14
  • 2017-09-18
  • 2019-04-10
相关资源
最近更新 更多