简体   繁体   English

如何在 Java 中将时间戳转换为日期?

[英]How to convert TimeStamp to Date in Java?

How do I convert 'timeStamp' to date after I get the count in Java?在获得 Java 中的计数后,如何将“timeStamp”转换为date

My current code is as follows:我目前的代码如下:

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

This is my database:这是我的数据库:
在此处输入图像描述

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?这里我得到的 output 是 2012-08-07 0,但等效查询返回 3。为什么我得到 0?

How do I convert 'timeStamp' to date after I get the count in java?在Java中获得计数后,如何将“ timeStamp”转换为date

My current code is as follows:我当前的代码如下:

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

This is my database:这是我的数据库:在此处输入图片说明

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?在这里,我得到的输出是2012-08-07 0,但是等效查询返回3。为什么我得到0?

How do I convert 'timeStamp' to date after I get the count in java?在Java中获得计数后,如何将“ timeStamp”转换为date

My current code is as follows:我当前的代码如下:

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

This is my database:这是我的数据库:在此处输入图片说明

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?在这里,我得到的输出是2012-08-07 0,但是等效查询返回3。为什么我得到0?

How do I convert 'timeStamp' to date after I get the count in java?在Java中获得计数后,如何将“ timeStamp”转换为date

My current code is as follows:我当前的代码如下:

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

This is my database:这是我的数据库:在此处输入图片说明

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?在这里,我得到的输出是2012-08-07 0,但是等效查询返回3。为什么我得到0?

How do I convert 'timeStamp' to date after I get the count in java?在Java中获得计数后,如何将“ timeStamp”转换为date

My current code is as follows:我当前的代码如下:

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

This is my database:这是我的数据库:在此处输入图片说明

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?在这里,我得到的输出是2012-08-07 0,但是等效查询返回3。为什么我得到0?

How do I convert 'timeStamp' to date after I get the count in java?在Java中获得计数后,如何将“ timeStamp”转换为date

My current code is as follows:我当前的代码如下:

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

This is my database:这是我的数据库:在此处输入图片说明

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?在这里,我得到的输出是2012-08-07 0,但是等效查询返回3。为什么我得到0?

How do I convert 'timeStamp' to date after I get the count in java?在Java中获得计数后,如何将“ timeStamp”转换为date

My current code is as follows:我当前的代码如下:

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

This is my database:这是我的数据库:在此处输入图片说明

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?在这里,我得到的输出是2012-08-07 0,但是等效查询返回3。为什么我得到0?

How do I convert 'timeStamp' to date after I get the count in java?在Java中获得计数后,如何将“ timeStamp”转换为date

My current code is as follows:我当前的代码如下:

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

This is my database:这是我的数据库:在此处输入图片说明

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?在这里,我得到的输出是2012-08-07 0,但是等效查询返回3。为什么我得到0?

How do I convert 'timeStamp' to date after I get the count in java?在Java中获得计数后,如何将“ timeStamp”转换为date

My current code is as follows:我当前的代码如下:

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

This is my database:这是我的数据库:在此处输入图片说明

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?在这里,我得到的输出是2012-08-07 0,但是等效查询返回3。为什么我得到0?

How do I convert 'timeStamp' to date after I get the count in java?在Java中获得计数后,如何将“ timeStamp”转换为date

My current code is as follows:我当前的代码如下:

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

This is my database:这是我的数据库:在此处输入图片说明

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?在这里,我得到的输出是2012-08-07 0,但是等效查询返回3。为什么我得到0?

How do I convert 'timeStamp' to date after I get the count in java?在Java中获得计数后,如何将“ timeStamp”转换为date

My current code is as follows:我当前的代码如下:

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

This is my database:这是我的数据库:在此处输入图片说明

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?在这里,我得到的输出是2012-08-07 0,但是等效查询返回3。为什么我得到0?

How do I convert 'timeStamp' to date after I get the count in java?在Java中获得计数后,如何将“ timeStamp”转换为date

My current code is as follows:我当前的代码如下:

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

This is my database:这是我的数据库:在此处输入图片说明

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?在这里,我得到的输出是2012-08-07 0,但是等效查询返回3。为什么我得到0?

How do I convert 'timeStamp' to date after I get the count in java?在Java中获得计数后,如何将“ timeStamp”转换为date

My current code is as follows:我当前的代码如下:

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

This is my database:这是我的数据库:在此处输入图片说明

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?在这里,我得到的输出是2012-08-07 0,但是等效查询返回3。为什么我得到0?

How do I convert 'timeStamp' to date after I get the count in java?在Java中获得计数后,如何将“ timeStamp”转换为date

My current code is as follows:我当前的代码如下:

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

This is my database:这是我的数据库:在此处输入图片说明

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?在这里,我得到的输出是2012-08-07 0,但是等效查询返回3。为什么我得到0?

How do I convert 'timeStamp' to date after I get the count in java?在Java中获得计数后,如何将“ timeStamp”转换为date

My current code is as follows:我当前的代码如下:

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

This is my database:这是我的数据库:在此处输入图片说明

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?在这里,我得到的输出是2012-08-07 0,但是等效查询返回3。为什么我得到0?

Date updated = timestamp.toDate();更新日期 = timestamp.toDate();

import java.sql.Timestamp

new Timestamp(1234567890123L)
// java.sql.Timestamp = 2009-02-14 10:31:30.123

(new Timestamp(1234567890123L)).toLocalDateTime().toLocalDate().toString()
// 2009-02-14

What is a different result from one of the previous most voted answers:与以前投票最多的答案之一有什么不同的结果:

import java.time.format.DateTimeFormatter
import java.time.ZoneId
import java.time.ZoneOffset

(DateTimeFormatter.ISO_LOCAL_DATE).withZone(ZoneId.from(ZoneOffset.UTC)).format(new Timestamp(1234567890123L).toInstant())
// String = 2009-02-13

In my case, I was expecting the "2009-02-14" instead of the "2009-02-13".就我而言,我期待的是“2009-02-14”而不是“2009-02-13”。 So the top result worked better for me.所以最好的结果对我来说效果更好。

How do I convert 'timeStamp' to date after I get the count in java?在Java中获得计数后,如何将“ timeStamp”转换为date

My current code is as follows:我当前的代码如下:

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

This is my database:这是我的数据库:在此处输入图片说明

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?在这里,我得到的输出是2012-08-07 0,但是等效查询返回3。为什么我得到0?

How do I convert 'timeStamp' to date after I get the count in java?在Java中获得计数后,如何将“ timeStamp”转换为date

My current code is as follows:我当前的代码如下:

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

This is my database:这是我的数据库:在此处输入图片说明

Here the output I got was 2012-08-07 0, but the equivalent query returns 3. Why do I get 0?在这里,我得到的输出是2012-08-07 0,但是等效查询返回3。为什么我得到0?

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

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