Java 中的 JDBC 时间戳转义格式和 SimpleDateFormat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11740645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:12:07  来源:igfitidea点击:

JDBC timestamp escape format and SimpleDateFormat in Java

javatimestampsimpledateformat

提问by ejboy

I'm trying to come up with SimpleDateFormatpattern to parse and format JDBC timestamps, in particular dates in the following format yyyy-mm-dd hh:mm:ss.fffffffff, where ffffffffffindicates nanoseconds.

我试图想出SimpleDateFormat模式来解析和格式化 JDBC 时间戳,特别是以下格式的日期yyyy-mm-dd hh:mm:ss.fffffffff,其中ffffffffff表示纳秒。

Unfortunately new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSS000000")does not work, the following exception is thrown:

不幸的是new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSS000000")不起作用,抛出以下异常:

java.text.ParseException: Format.parseObject(String) failed

Is this possible with SimpleDateFormat at all, or I have to create a custom subclass of java.text.DateFormat to handle this case? Please notethat it's not a question on how to parse yyyy-mm-dd hh:mm:ss.fffffffffstring in Java, I'm interested in a declarative approach, i.e. SimpleDateFormat pattern which does not require additional modifications of the input string .

SimpleDateFormat 是否可以做到这一点,或者我必须创建一个 java.text.DateFormat 的自定义子类来处理这种情况?请注意,这不是关于如何yyyy-mm-dd hh:mm:ss.fffffffff在 Java 中解析字符串的问题,我对声明式方法感​​兴趣,即不需要额外修改输入字符串的 SimpleDateFormat 模式。

Example:

示例

I expect the input 2012-02-05 17:00:34.427000000to be parsed as java.util.Date where milliseconds part is 427.

我希望输入2012-02-05 17:00:34.427000000被解析为 java.util.Date 其中毫秒部分是427

Here is a list of formats I've tried so far and they all failedfor various reasons:

这是我迄今为止尝试过的格式列表,但由于各种原因它们都失败了:

  • new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSS000000")- java.text.ParseException: Format.parseObject(String) failed
  • Both new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSSSS", Locale.US)and new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US)- are parsed as Fri Feb 10 15:37:14rather than expected one Sun Feb 05 17:00:34. (The nanoseconds part of 427000000is treated as milliseconds, even if only SSS is specified)
  • new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSS000000")—— java.text.ParseException: Format.parseObject(String) failed
  • 这两个new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSSSS", Locale.US)new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US)-被解析为周五2月10日15时37分14秒,而不是预期的一个太阳2月5日17时00分34秒。(427000000的纳秒部分被视为毫秒,即使只指定了 SSS)

采纳答案by minus

I know this is a late answer, but I have been looking into a similar problem and I came across this question.

我知道这是一个迟到的答案,但我一直在研究一个类似的问题,我遇到了这个问题。

The issue here is that SimpleDateFormatis expecting a java.util.Date, which does not have a nanosecond field. So, it does not handle that case. The java.sql.Timestampdocumentation says that it is a composite of java.util.Dateand a nanosecond field. The date/time value to the secondis stored in the Date portion of the object. Meaning that the remaining milliseconds can be ignored in favor of the nanosecond field. In my opinion, this is bad design because java.sql.Timestampis a direct subclass of java.util.Date.

这里的问题SimpleDateFormat是期待 a java.util.Date,它没有纳秒字段。所以,它不处理这种情况。该java.sql.Timestamp中的文件说,它是一个复合java.util.Date和纳秒场。的日期/时间值存储在对象的日期部分。这意味着可以忽略剩余的毫秒以支持纳秒字段。在我看来,这是一个糟糕的设计,因为它java.sql.Timestampjava.util.Date.

In any case, to answer the question, I don't think that it is possible to print the Timestamp with nanoseconds without appending to the String produced by SimpleDateFormat.

无论如何,为了回答这个问题,我认为在不附加到由SimpleDateFormat.

Here is one way to do it:

这是一种方法:

    Timestamp timestamp = new Timestamp(System.currentTimeMillis());
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.");
    format.setTimeZone(TimeZone.getDefault()); // Set the TimeZone to whatever you are expecting.

    System.out.println(format.format(timestamp) + String.format("%09d", timestamp.getNanos());   

EDIT:

编辑:

Added padding for the nanoseconds String value.

为纳秒字符串值添加了填充。

回答by Jon Skeet

EDIT: Now that you've given us a data sample, it's reasonably simple:

编辑:既然你已经给了我们一个数据样本,它相当简单:

"yyyy-MM-dd HH:mm:ss.SSS"

That certainly parses the value you've given. You may want to explicitly specify Locale.USas well, just so it doesn't try to use different separators...

这肯定会解析您给出的值。您可能还想明确指定Locale.US,这样它就不会尝试使用不同的分隔符...

EDIT: The trailing data beyond milliseconds causes issues. However, the early part of the data is fixed length (23 characters, I believe) so you should be able to write:

编辑:超过毫秒的尾随数据会导致问题。但是,数据的早期部分是固定长度的(我相信是 23 个字符),因此您应该可以这样写:

Date date = format.parse(text.substring(0, 23));

回答by Buhake Sindi

Date pattern for RFC3339is of format:

RFC3339 的日期模式格式为:

yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

Unless you have a different date format coming from a resource provider.'

除非您有来自资源提供者的不同日期格式。

If you have fractional seconds:

如果您有小数秒:

yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'


EDIT: Based on your example, the format I got to parse your string is:

编辑:根据你的例子,我解析你的字符串的格式是:

DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSSSS");