• <legend id='nDTR9'><style id='nDTR9'><dir id='nDTR9'><q id='nDTR9'></q></dir></style></legend>

      <i id='nDTR9'><tr id='nDTR9'><dt id='nDTR9'><q id='nDTR9'><span id='nDTR9'><b id='nDTR9'><form id='nDTR9'><ins id='nDTR9'></ins><ul id='nDTR9'></ul><sub id='nDTR9'></sub></form><legend id='nDTR9'></legend><bdo id='nDTR9'><pre id='nDTR9'><center id='nDTR9'></center></pre></bdo></b><th id='nDTR9'></th></span></q></dt></tr></i><div id='nDTR9'><tfoot id='nDTR9'></tfoot><dl id='nDTR9'><fieldset id='nDTR9'></fieldset></dl></div>
        • <bdo id='nDTR9'></bdo><ul id='nDTR9'></ul>
      1. <tfoot id='nDTR9'></tfoot>

        <small id='nDTR9'></small><noframes id='nDTR9'>

        6位微/毫秒的java日期格式

        时间:2023-09-29

        • <bdo id='dtZkl'></bdo><ul id='dtZkl'></ul>

            <small id='dtZkl'></small><noframes id='dtZkl'>

                <tfoot id='dtZkl'></tfoot>
                <legend id='dtZkl'><style id='dtZkl'><dir id='dtZkl'><q id='dtZkl'></q></dir></style></legend>
                • <i id='dtZkl'><tr id='dtZkl'><dt id='dtZkl'><q id='dtZkl'><span id='dtZkl'><b id='dtZkl'><form id='dtZkl'><ins id='dtZkl'></ins><ul id='dtZkl'></ul><sub id='dtZkl'></sub></form><legend id='dtZkl'></legend><bdo id='dtZkl'><pre id='dtZkl'><center id='dtZkl'></center></pre></bdo></b><th id='dtZkl'></th></span></q></dt></tr></i><div id='dtZkl'><tfoot id='dtZkl'></tfoot><dl id='dtZkl'><fieldset id='dtZkl'></fieldset></dl></div>
                    <tbody id='dtZkl'></tbody>
                  本文介绍了6位微/毫秒的java日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  从 Postgres 数据库中,我得到一个 6 位数的微秒(实际上是一个),例如2014-11-10 04:05:06.999999

                  现在,当我应用yyyy-MM-dd HH:mm:ss.SSS"的日期格式时,它会将 999999 转换为相应的秒/分钟,从而导致日期不正确.请参阅下面的代码片段

                  String dt = "2014-11-10 04:05:06.999999";String timeseriesFormat = "yyyy-MM-dd HH:mm:ss.SSS";SimpleDateFormat dateFormat = new SimpleDateFormat(timeseriesFormat);日期日期 = dateFormat.parse(dt);System.out.println(dateFormat.format(date));

                  结果于 2014 年 11 月 10 日 04:21:45.999

                  我想截断最后 3 位数字并保留 2014-11-10 04:05:06.999 的日期.如何截断它?我不想使用任何框架,如 joda 等.

                  解决方案

                  java.time

                  使用 Java 8 及更高版本中内置的 java.time 类.永远不要使用糟糕的遗留日期时间类,例如 DateCalendarSimpleDateFormatGregorianCalendar时间戳.

                  从 JDBC 4.2 起,我们可以直接与数据库交换 java.time 对象.

                  String input = "2014-11-10 04:05:06.999999".replace( " " , "T") ;//从 SQL 样式转换为 ISO 8601 标准格式,其中 `T` 将年-月-日部分与时-分-秒部分分开.LocalDateTime ldt = LocalDateTime.parse( 输入 ) ;

                  没有一刻

                  请注意,


                  关于java.time

                  java.time 框架内置于 Java 8 及更高版本中.这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.Date, 日历, &SimpleDateFormat.

                  要了解更多信息,请参阅 Oracle 教程.并在 Stack Overflow 上搜索许多示例和解释.规范是 JSR 310.

                  Joda-Time 项目,现在在 维护模式,建议迁移到 java.time 类.

                  您可以直接与您的数据库交换 java.time 对象.使用符合 JDBC 驱动程序/jeps/170" rel="nofollow noreferrer">JDBC 4.2 或更高版本.不需要字符串,不需要 java.sql.* 类.休眠 5 和JPA 2.2 支持 java.time.

                  从哪里获取 java.time 类?

                  • Java SE 8Java SE 9, Java SE 10, Java SE 11 及更高版本 - 具有捆绑实现的标准 Java API 的一部分.
                    • Java 9 带来了一些小功能和修复.
                  • Java SE 6Java SE 7
                    • 大部分 java.time 功能都向后移植到 Java 6 &7 在 ThreeTen-Backport.
                  • Android
                    • java.time 类的更高版本的 Android (26+) 捆绑实现.
                    • 对于早期的 Android (<26),称为 的进程API 脱糖 带来了 的子集java.time 功能最初并未内置于 Android 中.
                      • 如果脱糖不能提供您所需要的,ThreeTenABP 项目适应 ThreeTen-Backport(如上所述)到 Android.请参阅如何使用 ThreeTenABP….

                  From the Postgres Database I am getting a 6 digit microsecond (its actually a ) e.g. 2014-11-10 04:05:06.999999

                  now when i apply a date format of "yyyy-MM-dd HH:mm:ss.SSS" it converts the 999999 to corresponding seconds/minutes resulting in incorrect date. See code snippet below

                  String dt = "2014-11-10 04:05:06.999999";           
                          String timeseriesFormat = "yyyy-MM-dd HH:mm:ss.SSS";            
                          SimpleDateFormat dateFormat = new SimpleDateFormat(timeseriesFormat);
                          Date date = dateFormat.parse(dt);
                          System.out.println(dateFormat.format(date));
                  

                  results in 2014-11-10 04:21:45.999

                  I want to rather truncate the last 3 digits and keep a date of 2014-11-10 04:05:06.999. How to truncate it? I do not want to use any framework like joda etc.

                  解决方案

                  java.time

                  Use java.time classes, built into Java 8 and later. Never use the terrible legacy date-time classes such as Date, Calendar, SimpleDateFormat, GregorianCalendar, and Timestamp.

                  As of JDBC 4.2 and later, we can directly exchange java.time objects with the database.

                  String input = "2014-11-10 04:05:06.999999".replace( " " , "T" ) ; // Convert from SQL style to ISO 8601 standard format where `T` separates the year-month-day portion from hour-minute-second portion.
                  LocalDateTime ldt = LocalDateTime.parse( input ) ;
                  

                  Not a moment

                  Be aware that a LocalDateTime is not a moment, is not a point on the timeline. Your input lacks the context of a time zone or offset-from-UTC. So we do not know if your mean 4 AM on the 10th in Tokyo Japan, 4 AM in Toulouse France, or 4 AM in Toledo Ohio US — three very different moments, several hours apart on the timeline.

                  So you would be storing the LocalDateTime object’s value in a Postgres database column of type TIMESTAMP WITHOUT TIME ZONE.

                  A moment

                  If your intent was to represent a moment, a specific point on the timeline, you most know for certain the intended time zone.

                  Let's say you know the 4 AM was meant to be in Tokyo Japan time zone. Apply a ZoneId to the LocalDateTime to get a ZonedDateTime object.

                  ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
                  ZonedDateTime zdt = ldt.atZone( z ) ;
                  

                  Unfortunately, the JDBC 4.2 spec does not require support for the two most commonly used types, Instant and ZonedDateTime. No matter, we can easily convert to OffsetDateTime, for which support is required by JDBC spec.

                  OffsetDateTime odt = zdt.toOffsetDateTime() ;
                  

                  Such a value, a specific point on the timeline, should be written to a database column of type TIMESTAMP WITH TIME ZONE.

                  Write to the database.

                  myPreparedStatement.setObject( … , odt ) ;
                  

                  And retrieval.

                  myResultSet.getObject( … , OffsetDateTime.class ) ;
                  

                  java.sql.Timestamp

                  If you cannot move to Java 8, then you should be using your JDBC driver to get a java.sql.Timestamp object. That class is a hack, but it works. It is a java.util.Date but tracks the fractional seconds to resolution of nanoseconds instead of milliseconds. So it will preserve the microseconds used by Postgres.

                  You can use it as a java.util.Date but in other contexts you'll lose your extra resolution.

                  But are you really stuck with Java 6, 7, or earlier in the year 2020 now? Best to move to either Java 8 or Java 11, the two LTS versions.



                  About java.time

                  The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

                  To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

                  The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

                  You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

                  Where to obtain the java.time classes?

                  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
                    • Java 9 brought some minor features and fixes.
                  • Java SE 6 and Java SE 7
                    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
                  • Android
                    • Later versions of Android (26+) bundle implementations of the java.time classes.
                    • For earlier Android (<26), a process known as API desugaring brings a subset of the java.time functionality not originally built into Android.
                      • If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….

                  这篇关于6位微/毫秒的java日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将日期从 8 月 14 日格式化为 YYYYMMDD 下一篇:如何转换字符串“2011-11-29 12:34:25"迄今为止在

                  相关文章

                  最新文章

                  • <bdo id='i4ohY'></bdo><ul id='i4ohY'></ul>
                    <i id='i4ohY'><tr id='i4ohY'><dt id='i4ohY'><q id='i4ohY'><span id='i4ohY'><b id='i4ohY'><form id='i4ohY'><ins id='i4ohY'></ins><ul id='i4ohY'></ul><sub id='i4ohY'></sub></form><legend id='i4ohY'></legend><bdo id='i4ohY'><pre id='i4ohY'><center id='i4ohY'></center></pre></bdo></b><th id='i4ohY'></th></span></q></dt></tr></i><div id='i4ohY'><tfoot id='i4ohY'></tfoot><dl id='i4ohY'><fieldset id='i4ohY'></fieldset></dl></div>
                    <legend id='i4ohY'><style id='i4ohY'><dir id='i4ohY'><q id='i4ohY'></q></dir></style></legend><tfoot id='i4ohY'></tfoot>

                      <small id='i4ohY'></small><noframes id='i4ohY'>