英文:
Parsing string to Timestamp with 7 decimals, datetime format
问题
我正在尝试解析一个字符串并将其更改为时间戳。但是,我还没有找到正确的日期时间格式。传递的时间字符串如下:
2020-07-23T02:46:04.0978382Z
我找到的SimpleDateFormat的格式与这个字符串不完全匹配。我使用的最接近的一个格式使其相差几分钟和几秒钟,这似乎不正确。
我尝试过以下格式。格式和format6都能解析,但时间戳完全不对:
def timestampTest() :Unit ={
val timestampString = "2020-07-23T02:46:04.0978382Z" // 来自实际事件
val format = "yyyy-MM-dd'T'HH:mm:ss.SSS" // 解析但时间不正确: 2020-07-23 03:02:22.382
val format1= "yyyy-MM-dd'T'HH:mm:ss.SSSZ" // 无法解析
val format2= "yyyy-MM-dd'T'HH:mm:ss.SSSX" // 无法解析
val format3= "yyyy-MM-dd'T'HH:mm:ss.SSSXX" // 无法解析
val format4 = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" // 无法解析
val format5 = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ" // 无法解析
val format6 = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS"// 时间不正确: 2020-07-23 03:02:22.382
val dateFormat = new SimpleDateFormat(format)
val x = new Timestamp(dateFormat.parse(timestampString).getTime)
println(timestampString)
println(x)
}
是否有支持这种字符串的日期时间格式?或者看起来我收到的字符串是否需要进行调整/修整?
英文:
I am trying to parse a string and change it to a timestamp. However, I haven't been able to find the right datetime format. The time string that is being passed is this:
2020-07-23T02:46:04.0978382Z
The formats that I've found for SimpleDateFormat don't exactly match this string. The closest one I've used, makes it off by minutes and seconds, which doesn't seem right.
I've tried the following formats. Format, and format6 parse it but the timestamp is completely off:
def timestampTest() :Unit ={
val timestampString = "2020-07-23T02:46:04.0978382Z" // from actual event
val format = "yyyy-MM-dd'T'HH:mm:ss.SSS" // parses but makes it off by seconds, min, hour: 2020-07-23 03:02:22.382
val format1= "yyyy-MM-dd'T'HH:mm:ss.SSSZ" // Cant parse
val format2= "yyyy-MM-dd'T'HH:mm:ss.SSSX" // cant parse
val format3= "yyyy-MM-dd'T'HH:mm:ss.SSSXX" // cant parse
val format4 = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" // cant parse
val format5 = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ" // cant parse
val format6 = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS"// Time is off: 2020-07-23 03:02:22.382
val dateFormat = new SimpleDateFormat(format)
val x = new Timestamp(dateFormat.parse(timestampString).getTime)
println(timestampString)
println(x)
}
Is there a datetime format that supports such string? Or does it look like the string I'm receiving will have be adjusted/trimmed.
答案1
得分: 2
以下是翻译的内容:
有支持这种字符串的日期时间格式吗?
是的,您可以使用 ZonedDateTime
或 OffsetDateTime
或 Instant
,它们默认解析您问题中的日期时间字符串格式(即不需要显式使用任何 DateTimeFormatter.ofPattern
)。
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
ZonedDateTime zdt = ZonedDateTime.parse("2020-07-23T02:46:04.0978382Z");
System.out.println(zdt);
OffsetDateTime odt = OffsetDateTime.parse("2020-07-23T02:46:04.0978382Z");
System.out.println(odt);
Instant instant = Instant.parse("2020-07-23T02:46:04.0978382Z");
System.out.println(instant);
}
}
输出:
2020-07-23T02:46:04.097838200Z
2020-07-23T02:46:04.097838200Z
2020-07-23T02:46:04.097838200Z
我建议您从过时且容易出错的传统日期时间 API 切换到现代日期时间 API。
使用 SimpleDateFormat
有什么问题?
SimpleDateFormat
不支持纳秒。它只支持毫秒,这就是为什么您的输出时间不准确的原因。
我可以直接在 JDBC 中使用 OffsetDateTime
吗?
是的,当然可以。查看此页面以获取更多信息。以下是同样内容的屏幕截图(以防链接在将来失效):
我可以从 OffsetDateTime
获取 Timestamp
吗?
是的,您可以(如下所示),但我不建议这样做,因为它继承了设计不佳的 java.util.Date
。正如前面建议的,您可以直接在 JDBC 中使用 OffsetDateTime
,并且应该坚持使用现代日期时间 API。
import java.sql.Timestamp;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("2020-07-23T02:46:04.0978382Z");
System.out.println(odt);
Timestamp ts = Timestamp.valueOf(odt.atZoneSameInstant(ZoneOffset.UTC).toLocalDateTime());
System.out.println(ts);
}
}
输出:
2020-07-23T02:46:04.097838200Z
2020-07-23 02:46:04.0978382
英文:
> Is there a datetime format that supports such string?
Yes, you can use ZonedDateTime
or OffsetDateTime
or Instant
which parses the date-time string format of your question by default (i.e. without using any DateTimeFormatter.ofPattern
explicitly).
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
ZonedDateTime zdt = ZonedDateTime.parse("2020-07-23T02:46:04.0978382Z");
System.out.println(zdt);
OffsetDateTime odt = OffsetDateTime.parse("2020-07-23T02:46:04.0978382Z");
System.out.println(odt);
Instant instant = Instant.parse("2020-07-23T02:46:04.0978382Z");
System.out.println(instant);
}
}
Output:
2020-07-23T02:46:04.097838200Z
2020-07-23T02:46:04.097838200Z
2020-07-23T02:46:04.097838200Z
I suggest you switch from the outdated and error-prone legacy date-time API to the modern date-time API.
What went wrong with using SimpleDateFormat
?
SimpleDateFormat
doesn't support nanoseconds. It supports only up to milliseconds and that's the reason why time was off in your output.
Can I use OffsetDateTime
directly in JDBC?
Yes, of course. Check this page for more information. Give below is the screenshot of the same (in case the link gets broken in the future):
Can I get Timestamp
from OffsetDateTime
?
Yes, you can (as shown below) but I do not suggest it as it inherits the horribly designed java.util.Date
. As suggested earlier, you can use OffsetDateTime
directly with JDBC and you should stick to the modern date-time API.
import java.sql.Timestamp;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("2020-07-23T02:46:04.0978382Z");
System.out.println(odt);
Timestamp ts = Timestamp.valueOf(odt.atZoneSameInstant(ZoneOffset.UTC).toLocalDateTime());
System.out.println(ts);
}
}
Output:
2020-07-23T02:46:04.097838200Z
2020-07-23 02:46:04.0978382
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论