java - 从数据库检索的时间为 00 :00:00

标签 java mysql web-applications

我的 MySQL 数据库中有一个数据类型为日期时间的列,但是当我从该列检索数据时,它显示时间 00:00:00,而不是我存储的时间。

public class MainWork extends TimerTask {

private static Final DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 公共(public)无效运行(){

    try{

        Class.forName("com.mysql.cj.jdbc.Driver");  
        Connection con =DriverManager.getConnection(  
        "jdbc:mysql://localhost:3306/reminder","sarthak","sar31thak");  
        Statement stmt=con.createStatement();

        ResultSet rs= stmt.executeQuery("select * from ToDo");
        while(rs.next()){
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date dt = new Date();
            dt=rs.getDate(2);
            String activity=rs.getString(1);
            String str= sdf.format(dt); 
            Date localDate = new Date();
            String today;
            today=sdf.format(localDate);
            if(str.equals(today)){

                JOptionPane optionPane = new JOptionPane(activity,JOptionPane.WARNING_MESSAGE);
                JDialog dialog = optionPane.createDialog("REMINDER");
                //Toolkit.getDefaultToolkit().beep();
                dialog.setAlwaysOnTop(true); // to show top of all other application
                dialog.setVisible(true); // to visible the dialog

            }
            else
            {
             continue;

            }
        }
    }
    catch (Exception e)
    {System.out.println("Got an ERROR");}
}

public static void main(String args[]){
    Timer t1 = new Timer();
    t1.schedule(new MainWork(), 0,60000);


}

}

最佳答案

不起作用,因为您仅获得日期并转换为“yyyy/MM/dd HH:mm:ss”。这就是为什么您会得到 00:00:00

使用getDate()仅返回日期

你应该尝试一下,

rs.getTimestamp();

关于java - 从数据库检索的时间为 00 :00:00,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54998763/

相关文章:

java - 接收表单数据

Java AffinetransformOp filter() 搞砸了我的形象

mysql - SQL 按字符串末尾的数字排序

c# - 使用 C# 和 AllowZeroDatetime 从 MySQL 表中获取日期时间

java - 为什么总是显示找不到文件

java - 限制 Joda 定时器不依赖于系统时间

php - 如何在日期列上添加 MySQL 中的天数?

java - 更改 Web 应用程序语言

java - Java 的反向 ajax 库是什么?

javascript - 具有不同模板的 Web 应用程序 (AngularJS)