java 到 mysql。我需要从字符串参数转换为时间戳
java to mysql. I need convert from string parametre to timestamp
我正在尝试将字符串解析为时间戳,因为我需要将此数据保存在 bbdd mysql 上。
String dateString: "2018-10-17T22:37:10.000+0000";
java.sql.Timestamp timeStampDate = null;
try {
DateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Date date = (Date) formatter.parse(dateString);
timeStampDate = new Timestamp(date.getTime());
} catch (ParseException e) {
log.debug("ERROR parser String to Timestamp to save bbdd. ", e.getMessage());
}
当我 运行 我的应用程序时,我收到这条捕获消息:
ERROR parser String to Timestamp to save bbdd. Unparseable date: "2018-10-17T22:37:10.000+0000"
有人可以帮我吗?
把你的面具改成
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
所以你有
java.sql.Timestamp timeStampDate = null;
String dateString = "2018-10-17T22:37:10.000+0000";
try {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = formatter.parse(dateString);
timeStampDate = new Timestamp(date.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
顺便说一下 你应该不需要 cast
Date
为我的懈怠道歉,在我匆忙中我没有测试输出并且根据@andreas评论,正确的掩码实际上是yyyy-MM-dd'T'HH:mm:ss.SSSZ
java.time
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXX");
String dateString = "2018-10-17T22:37:10.000+0000";
OffsetDateTime odt = OffsetDateTime.parse(dateString, formatter);
System.out.println("Parsed datetime: " + odt);
此代码的输出是:
Parsed datetime: 2018-10-17T22:37:10Z
为了保存到 MySQL 中,使用 datetime 对象很好,但是 Timestamp
class 存在设计问题,现在已经过时了。很抱歉,我没有使用 MySQL 的经验,但我认为以下方法应该有效:
PreparedStatement ps = myDatabaseConnection.prepareStatement(
"insert into my_table (my_timestamp) values (?)");
ps.setObject(1, odt);
Link: Oracle tutorial: Date Time 解释如何使用 java.time
.
我正在尝试将字符串解析为时间戳,因为我需要将此数据保存在 bbdd mysql 上。
String dateString: "2018-10-17T22:37:10.000+0000";
java.sql.Timestamp timeStampDate = null;
try {
DateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Date date = (Date) formatter.parse(dateString);
timeStampDate = new Timestamp(date.getTime());
} catch (ParseException e) {
log.debug("ERROR parser String to Timestamp to save bbdd. ", e.getMessage());
}
当我 运行 我的应用程序时,我收到这条捕获消息:
ERROR parser String to Timestamp to save bbdd. Unparseable date: "2018-10-17T22:37:10.000+0000"
有人可以帮我吗?
把你的面具改成
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
所以你有
java.sql.Timestamp timeStampDate = null;
String dateString = "2018-10-17T22:37:10.000+0000";
try {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = formatter.parse(dateString);
timeStampDate = new Timestamp(date.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
顺便说一下 你应该不需要 cast
Date
为我的懈怠道歉,在我匆忙中我没有测试输出并且根据@andreas评论,正确的掩码实际上是yyyy-MM-dd'T'HH:mm:ss.SSSZ
java.time
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXX");
String dateString = "2018-10-17T22:37:10.000+0000";
OffsetDateTime odt = OffsetDateTime.parse(dateString, formatter);
System.out.println("Parsed datetime: " + odt);
此代码的输出是:
Parsed datetime: 2018-10-17T22:37:10Z
为了保存到 MySQL 中,使用 datetime 对象很好,但是 Timestamp
class 存在设计问题,现在已经过时了。很抱歉,我没有使用 MySQL 的经验,但我认为以下方法应该有效:
PreparedStatement ps = myDatabaseConnection.prepareStatement(
"insert into my_table (my_timestamp) values (?)");
ps.setObject(1, odt);
Link: Oracle tutorial: Date Time 解释如何使用 java.time
.