java日期格式,6位微/毫秒 [英] java date format with 6 digit micro/milli second
问题描述
从Postgres数据库我得到一个6位数的微秒(实际上是a),例如2014-11-10 04:05:06。 999999
From the Postgres Database I am getting a 6 digit microsecond (its actually a ) e.g. 2014-11-10 04:05:06.999999
现在当我申请日期格式为yyyy-MM-dd HH时: mm:ss.SSS它将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));
结果2014-11-10 04:21:45.999
results in 2014-11-10 04:21:45.999
我想要截断最后3位数并保持 2014-11-10 04:05:06.999 的日期。
如何截断它?我不想使用像joda等任何框架。
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.sql.Timestamp
您应该使用JDBC驱动程序来获取 java.sql.Timestamp
对象。那个类是一个黑客,但它的工作原理。它是一个java.util.Date,但跟踪分数秒到分辨率为纳秒而不是毫秒。因此它将保留Postgres使用的微秒。
java.sql.Timestamp
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.
您可以将它用作java.util.Date,但在其他情况下,您将失去额外的分辨率。
You can use it as a java.util.Date but in other contexts you'll lose your extra resolution.
提示:查看Java 8中内置的java.time包。它提供了完整的日期对象具有纳秒分辨率的时间库。 java.sql.Timestamp类具有来回转换的方法。
Tip: look at the java.time package built into Java 8. It provides a full date-time library whose objects have nanosecond resolution. The java.sql.Timestamp class has methods to convert back and forth.
请注意,在java.time中,格式化程序编码的解析模式字符串不适用于可变数量的小数第二位数。请参阅此问题, JSR-310 - 解析具有可变长度的秒分数以供讨论。解决方案是使用 DateTimeFormatterBuilder
对象来表示我们期望任意数量的零个,一个或多个数字(最多九个)为秒。
Note that in java.time a formatter coded parsing pattern string does not work for a variable number of fractional second digits. See this Question, JSR-310 - parsing seconds fraction with variable length for discussion. The solution is to use a DateTimeFormatterBuilder
object to say that we expect any number of zero, one, or more digits (up to nine) for a fractional second.
String input = "2014-11-10 04:05:06.999999";
DateTimeFormatter formatter = new DateTimeFormatterBuilder ()
.appendPattern ( "yyyy-MM-dd HH:mm:ss" )
.appendFraction ( ChronoField.NANO_OF_SECOND , 0 , 9 , true ) // Nanoseconds = 0-9 digits of fractional second.
.toFormatter ();
我们的输入字符串没有时区也没有从UTC偏移。因此,我们必须首先处理本地日期时间,而不是与时间轴上的时刻相关联。
Our input string has no time zone nor offset-from-UTC. So we must first process as a "local" date-time, not tied to a moment on the timeline.
LocalDateTime localDateTime = LocalDateTime.parse ( input , formatter );
让我们通过应用UTC的偏移将它与时间线联系起来。我将假设在本课题的情况下,预期的偏移量是UTC本身。
Let's tie it to the time line by applying an offset-from-UTC. I will assume that in the case of this Question the intended offset was UTC itself.
OffsetDateTime odt = localDateTime.atOffset ( ZoneOffset.UTC );
最后我们要转换为java.sql类型以实际与数据库通信。为此,我们必须在时间轴上获得 Instant
,按照定义始终为UTC。您可以将 OffsetDateTime
视为即时
加上与UTC的偏移量。要转换为java.sql,我们需要通过提取一个简单的 Instant
来剥离offset-from-UTC组件。
Finally we want to convert into a java.sql type for actually communicating with the database. To that we must get an Instant
, a moment on the timeline, always in UTC by definition. You can think of an OffsetDateTime
as being an Instant
plus an offset-from-UTC. To convert to java.sql we need to strip away the offset-from-UTC component by extracting just a simple Instant
.
Instant instant = odt.toInstant ();
java.sql.Timestamp ts = java.sql.Timestamp.from ( instant );
转储到控制台。
System.out.println ( "input: " + input + " | localDateTime: " + localDateTime + " | odt: " + odt + " | instant: " + instant + " | ts: " + ts );
输入:2014-11-10 04:05:06.999999 | localDateTime:2014-11-10T04:05:06.999999 | odt:2014-11-10T04:05:06.999999Z |时间:2014-11-10T04:05:06.999999Z | ts:2014-11-09 20:05:06.999999
input: 2014-11-10 04:05:06.999999 | localDateTime: 2014-11-10T04:05:06.999999 | odt: 2014-11-10T04:05:06.999999Z | instant: 2014-11-10T04:05:06.999999Z | ts: 2014-11-09 20:05:06.999999
这篇关于java日期格式,6位微/毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!