簡體   English   中英

從數據庫中檢索的時間是00:00:00

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

我的MySQL數據庫中有一個數據類型為datetime的列,但是當我從該列檢索數據時,它顯示的時間是00:00:00,而不是我存儲的時間。

public class MainWork extends TimerTask {

私有靜態最終DateFormat sdf = new SimpleDateFormat(“ yyyy / MM / dd HH:mm:ss”); 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);


}

}

無法正常工作,因為您僅獲得Date並轉換為"yyyy/MM/dd HH:mm:ss" 這就是為什么您得到00:00:00的原因。

使用getDate()僅返回Date

你應該試試,

rs.getTimestamp();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM