我正在尝试将String转换为Timestamp以使用Java Springboot保存在我的postgresql数据库中。 我面临的问题是我在这里看到许多不同主题的问题,但似乎我可能会遗漏一些东西而且我看不清究竟是什么。我的假设是SimpleDateFormat对象发生了错误,而且没有正确格式化日期。
这是我从String转换为Timestamp的函数。我在这里传递的字符串取自Android发送的Volley请求。
private Timestamp convertDate(String date){
try {
System.out.println("Passed date to method: " + date);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd HH:mm");
Date parsedDate = simpleDateFormat.parse(date);
System.out.println("Pre returned date: " + parsedDate);
Timestamp timestamp = new Timestamp(parsedDate.getTime());
System.out.println("Pre returned timestamp: " + timestamp);
return timestamp;
}catch (ParseException e){
return null;
}
}
这就是我的日志中的内容。
Passed date to method: 11/06 12:47
Pre returned date: Fri Nov 06 12:47:00 UTC 1970
Pre returned timestamp: 1970-11-06 12:47:00.0
非常感谢任何帮助。