【问题标题】:Error Timestamp format [duplicate]错误时间戳格式[重复]
【发布时间】:2014-08-11 16:13:09
【问题描述】:

我正在尝试从我的数据库中检索 Timestamp,我需要将其转换为 java.util.Date

我一直在关注我在 stackoverflow 中找到的几个示例,但我不断收到以下异常:

java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]

我不知道为什么,我已经尝试了几种方法,你能帮帮我吗?

这是我的代码:

//select all partitions from table "LDRS"
stmtSelect = connection.prepareCall("Select HIGH_VALUE from user_tab_partitions where table_name = 'LDRS'");

ResultSet rs = stmtSelect.executeQuery();

SimpleDateFormat myFormat = new SimpleDateFormat("dd.MM.yyyy");

while (rs.next()) {
    try {
        Timestamp ts = rs.getTimestamp("HIGH_VALUE");
        Date date1 = myFormat.parse(ts.toString());
        Date date2 = myFormat.parse(""+new Date());
        // compares the current date with the partition date, if the difference between them is more than 30 days
        // drops the partition
        long diff = date2.getTime() - date1.getTime(); 
...
}

【问题讨论】:

    标签: java oracle timestamp


    【解决方案1】:

    停止将值转换为字符串并返回。 java.sql.Timestamp 已经 java.util.Date

    long now = System.currentTimeMillis();
    
    while (rs.next()) {
        Timestamp ts = rs.getTimestamp("HIGH_VALUE");
        long diff = ts.getTime() - now;
        ... use diff ...
    }
    

    您应该在您根本想这样做时处理字符串。在这种情况下,“查找存储的时间戳和当前时间之间的差异”与字符串表示无关。

    【讨论】:

    • 对不起。我忘了告诉。我需要两个日期的天数差异,5 天,10 天等...我不能用磨坊来做,可以吗?
    • @MyNameIsRui:在哪里你得到那个异常?您没有提供堆栈跟踪,而我提供的代码不会 给出该问题,因为它不执行任何解析。至于转换为天 - 你需要考虑你想用时区做什么。如果您乐于将一天视为总是是 24 小时,您可以将毫秒除以“24 小时内的毫秒数” - 但这意味着您会得到奇怪的结果如果您考虑本地时间。
    • 没关系,我的错误,你的代码工作正常。 Tks
    猜你喜欢
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 2017-02-19
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多