java到mysql.我需要将字符串参数转换为时间戳 [英] java to mysql. I need convert from string parametre to timestamp

查看:90
本文介绍了java到mysql.我需要将字符串参数转换为时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将String解析为Timestamp,因为我需要将此数据保存在bbdd mysql上.

I'm trying to parser String to Timestamp because I need to save this data on 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());
    }

运行我的应用程序时,我收到以下捕获消息:

When I run my app I get this catch message:

错误解析器将字符串转换为时间戳以保存bbdd.无法解析的日期:"2018-10-17T22:37:10.000 + 0000"

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

顺便说一句,您无需播出日期

By the way you should not need to cast the Date

道歉,我很懒散,我匆忙没有测试输出,按照@andreas注释,正确的掩码实际上是 yyyy-MM-dd'T'HH:mm:ss.SSSZ

Apologies for my slackness, in my haste I did not test the output and as per @andreas comment, the correct mask is actually yyyy-MM-dd'T'HH:mm:ss.SSSZ

这篇关于java到mysql.我需要将字符串参数转换为时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆