我的 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/