当前位置: 首页 > 知识库问答 >
问题:

以微秒或纳秒精度解析Java数据

慎芷阳
2023-03-14
  • 2015-05-09 00:10:23.999750900//后9位表示纳秒

当通过模式解析时

  • YYYY-MM-DD HH:MM:SS.SSSSSSS//9“s”符号
    null
    null

有没有其他方法可以通过只向标准simpledateformat实现提供一个模式来获得正确的解决方案,而不需要任何其他代码修改或字符串操作?

共有1个答案

赖明煦
2023-03-14
LocalDateTime.parse(                 // With resolution of nanoseconds, represent the idea of a date and time somewhere, unspecified. Does *not* represent a moment, is *not* a point on the timeline. To determine an actual moment, place this date+time into context of a time zone (apply a `ZoneId` to get a `ZonedDateTime`). 
    "2015-05-09 00:10:23.999750900"  // A `String` nearly in standard ISO 8601 format.
    .replace( " " , "T" )            // Replace SPACE in middle with `T` to comply with ISO 8601 standard format.
)                                    // Returns a `LocalDateTime` object.

否,不能使用SimpleDateFormat处理纳秒。

但你的前提是…

Java在其日期模式中不支持超过毫秒的时间粒度

instant.toString()  // Generate a `String` representing this moment, using standard ISO 8601 format.
Instant instant = Instant.now() ;  // Capture the current moment. May be in milliseconds or microseconds rather than the maximum resolution of nanoseconds.

将这样的输入标记为LocalDateTime对象。首先,用t替换中间的空格,以符合ISO 8601格式,在解析/生成字符串时默认使用该格式。因此不需要指定格式模式。

LocalDateTime ldt = 
        LocalDateTime.parse( 
            "2015-05-09 00:10:23.999750900".replace( " " , "T" )  // Replace SPACE in middle with `T` to comply with ISO 8601 standard format.
        ) 
;

java.sql.timestamp类也处理纳秒级分辨率,但方式很尴尬。通常最好在java.time类中完成工作。从JDBC4.2和更高版本开始,就不需要再使用timestamp了。

myPreparedStatement.setObject( … , instant ) ;

和检索。

Instant instant = myResultSet.getObject( … , Instant.class ) ;
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ; 
myPreparedStatement.setObject( … , odt ) ;
Instant instant = myResultSet.getObject( … , OffsetDateTime.class ).toInstant() ;

您可以直接与数据库交换java.time对象。使用与JDBC4.2或更高版本兼容的JDBC驱动程序。不需要字符串,不需要java.sql.*类。

从哪里获取java.time类?

  • Java SE 8、Java SE 9、Java SE 10及更高版本
    • 内置。
    • 带有捆绑实现的标准Java API的一部分。
    • Java 9添加了一些小功能和修补程序。
      null
      null

 类似资料:
  • Cassandra表具有数据类型列,那么如何查看类型的纳秒值? timeuuid: 如何将此timeuuid转换为纳秒级需要一个select语句,如下所示:

  • 两个时间戳的示例 困难的部分原因是我认为没有类似的包来处理它。我不确定是否需要使用正则表达式来提取秒,然后将纳秒转换为毫秒。我愿意接受任何建议。 另外,您建议如何处理?我想我应该使用,然后像这样的包可以解析时间戳,甚至可以达到纳秒级的精度?

  • 这个问题我已经研究了一段时间,但我只能得到秒数,下面是给我的说明:编写一个程序,从代表纳秒数的标准输入中读取一个非负整数。然后,程序编写一行输出,给出等效的天、小时、分钟、秒、毫秒、微秒和纳秒的数量。例如,输入为123456789123456789。则输出为:123456789123456789纳秒等于1428天、21小时、33分钟、9秒、123毫秒、456微秒和789纳秒。这是我写的: }

  • 我正试图为我的项目标准化时间戳格式,其中源以微秒精度报告。我试图找出是否有一种干净的或者最小化的方法,不需要使用手写的常量。

  • 本文向大家介绍nodejs如何高精度计时(纳秒)?相关面试题,主要包含被问及nodejs如何高精度计时(纳秒)?时的应答技巧和注意事项,需要的朋友参考一下 const start = process.hrtime.bigint(); // 191051479007711n setTimeout(() => { const end = process.hrtime.bigint(); // 1910

  • 问题内容: 因此,我刚刚发现MySQL中最令人沮丧的错误。 显然,该字段及其支持功能不支持比秒更高的精度! 所以我正在使用PHP和Doctrine,我真的需要那些微秒(我正在使用属性)。 我发现我可以使用一个字段来存储值。但是学说会增加毫秒吗?我认为它只是将NOW()分配给该字段。我还担心通过代码散布的日期操作功能(在SQL中)会中断。 我还看到了有关编译UDF扩展的内容。这是不可接受的,因为我或