简体   繁体   English

从数据库中检索的时间是00:00:00

[英]Time retrieved from database is 00:00:00

I have a column in my MySQL database of datatype datetime but when I retrieve the data from this column it shows the time 00:00:00 instead of what I stored. 我的MySQL数据库中有一个数据类型为datetime的列,但是当我从该列检索数据时,它显示的时间是00:00:00,而不是我存储的时间。

public class MainWork extends TimerTask {

private static final DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 私有静态最终DateFormat sdf = new SimpleDateFormat(“ yyyy / MM / dd HH:mm:ss”); public void run(){ public void run(){

    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);


}

} }

Not working because you get only the Date and convert to "yyyy/MM/dd HH:mm:ss" . 无法正常工作,因为您仅获得Date并转换为"yyyy/MM/dd HH:mm:ss" So that's why you get 00:00:00 . 这就是为什么您得到00:00:00的原因。

Using getDate() only returns a Date . 使用getDate()仅返回Date

You should try, 你应该试试,

rs.getTimestamp();

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

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