converting timestamp to date in java

后端 未结 6 1101
既然无缘
既然无缘 2020-12-21 08:14

This is my database: \"enter

Here I have to check the query current date+status=Q info

6条回答
  •  醉话见心
    2020-12-21 08:58

    to print the timestamp in yyyy-mm-dd:

    Date date = new Date(timestamp);
    DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd");
    System.out.println( dateFormat.format (date));
    

    UPDATE

    here is a HINT of how you can proceed:

    public static int data() {
        int count = 0;
        Date now = Calendar.getInstance().getTime();
        System.out.println("TODAY IS:"+now.getTime()); //TODAY IS:1344007864862
    
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
    
            PreparedStatement statement1 = (PreparedStatement) con.prepareStatement("SELECT * FROM ORDERS WHERE STATUS = 'Q' AND DT= "+now.getTime());
            ResultSet rs1 = statement1.executeQuery();
            while (rs1 .next()) {
                count++; //A HIT IS FOUND
            }
        } catch (Exception exc) {
            System.out.println(exc.getMessage());
        }
        return count;
    }
    

    Obviously the now.getTime() returns the actual millisecond the date was captured in the now variable . The mathematics is all up to your implementation from now on.

    Remember that the way to get a Calendar object at midnight (10/05/2012 00:00:00) is here Java program to get the current date without timestamp

提交回复
热议问题