【问题标题】:timestamp to date in java and get the count valuejava中的时间戳到日期并获取计数值
【发布时间】:2012-08-05 03:46:12
【问题描述】:

您好,我必须在检查查询并返回计数值后将时间戳转换为日期。 我的数据库有日期(1344399208,1344399269),状态(Q,Q)。 这是我的代码:

public class GetCurrentDateTime {
 public int data(){
int count=0;
java.sql.Timestamp timeStamp =new Timestamp(System.currentTimeMillis());

          java.sql.Date      date      = new java.sql.Date(timeStamp.getTime()); 
           System.out.println(date);
//count++;


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

    PreparedStatement statement =  con.prepareStatement("select * from xcart_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)格式。它成功完成了。我得到的输出是 2012-08-08。但我必须检查查询today date+status=Q .so 该日期如何保存在变量中并在查询中调用该变量。so 如何为上述条件编写查询。检查条件并显示返回计数值我的tomcat控制台。怎么办。请帮帮我

【问题讨论】:

    标签: java mysql date jdbc timestamp


    【解决方案1】:

    部分回答您的问题

    日期示例

    从 Code Ranch 和 SO 帖子中借用的示例

    // Get system time
    
    Timestamp SysTime = new Timestamp(System.currentTimeMillis());
    
    java.util.Date UtilDate = new java.util.Date(Systime.getTime());
    java.sql.Date SQLDate = new java.sql.Date(Systime.getTime());
    
    // Date + Time + Nano Sec
    System.out.println(SysTime); 
    
    // Date + Time
    System.out.println(UtilDate); 
    
    // Date
    System.out.println(SQLDate); 
    

    格式化日期

    // Apply Format
    Date InDate = SQLDate; // or UtilDate
    DateFormat DateFormat = new SimpleDateFormat("yyyy MM dd");
    String DisplayDate = DateFormat.format(InDate);
    System.out.println(DisplayDate);
    

    请注意,我是 java 新手,因此请验证它是否有效。

    比较日期

    查看此 SO 帖子:

    How to compare dates using Java

    【讨论】:

    • 在我的代码中,我成功地得到了输出 2012-08-08。但我希望该日期保存在变量中。在查询中调用该变量。所以如何编写今天日期+状态的查询=Q condition.after 检查条件并在我的 tomcat 控制台上显示返回计数值。如何做。请帮助我
    • 请查看 SO 帖子的链接
    【解决方案2】:

    将日期转换为指定的日期格式:

    int timestamp = 1231342342342; // replace with timestamp fetched from DB
    Date date = new Date(timestamp);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
    String dateString = sdf.format(date); //convert to yyyy-mm-dd format
    

    根据我对编辑的理解,您希望查询是这样的:

    PreparedStatement statement =  con.prepareStatement("select * from xcart_orders where status='Q' AND date='"+dateString+"'");
    

    我假设日期以字符串格式存储在数据库中,因为您要求将其转换为特定格式。

    来自 cmets:

    获取午夜日期:

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(timestamp);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    

    要获取 24 周期内的所有条目:

    "select * from xcart_orders where status='Q' AND date between " + cal.getTimeInMillis() + " and " + (cal.getTimeInMillis() + 86400000l);
    

    【讨论】:

    • 在上面的代码中,我得到的输出是 08-08-2012 成功..但我希望今天需要查询 date+status=Q 检查这两个条件意味着它显示在我的计数值console.so 这里日期保存在变量中并调用该查询。
    • 您想要 08-08-2012 和 09-08-2012 之间的所有条目。整整 24 小时对吧?
    • 你在输出中得到了什么?
    • 仅显示 0。但匹配的查询为 2
    猜你喜欢
    • 2021-12-03
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    相关资源
    最近更新 更多