DateTime with "UTC" Timezone to DateTime only (without the UTC Timezone) conversion in Java

huangapple go评论65阅读模式
英文:

DateTime with "UTC" Timezone to DateTime only (without the UTC Timezone) conversion in Java

问题

以下是代码的翻译部分:

String s = "2023-02-13 06:10:45.483000 UTC";
TemporalAccessor ta = DateTimeFormatter.ISO_INSTANT.parse(s);
Instant i = Instant.from(ta);
Date d = Date.from(i);

System.out.println(d);

这是我的示例代码,它返回了一个Java错误:

Exception in thread "main" java.time.format.DateTimeParseException: Text '2023-02-13 06:10:45.483000 UTC' could not be parsed at index 10
        at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
        at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1880)
        at App.main(App.java:29)

希望这对你有所帮助。

英文:

I have this sample String "2023-02-13 06:10:45.483000 UTC" and I want to convert this into a simple DateTime format without the Timezone in Java. How do I do this in Java language?

String s = "2023-02-13 06:10:45.483000 UTC";
TemporalAccessor ta = DateTimeFormatter.ISO_INSTANT.parse(s);
Instant i = Instant.from(ta);
Date d = Date.from(i);

System.out.println(d);

This is my sample code and it returns an error from Java:

Exception in thread "main" java.time.format.DateTimeParseException: Text '2023-02-13 06:10:45.483000 UTC' could not be parsed at index 10
        at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
        at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1880)
        at App.main(App.java:29)

答案1

得分: 2

我能够使用以下代码解决我的问题:

private static final DateTimeFormatter BIG_QUERY_PARSER = new DateTimeFormatterBuilder()
        .append(DateTimeFormatter.ISO_LOCAL_DATE)
        .appendLiteral(' ')
        .append(DateTimeFormatter.ISO_LOCAL_TIME)
        .appendPattern(" zzz")
        .toFormatter(Locale.ENGLISH);

感谢您的评论和解决方案。

英文:

I was able to solve my issue using

private static final DateTimeFormatter BIG_QUERY_PARSER = new DateTimeFormatterBuilder()
        .append(DateTimeFormatter.ISO_LOCAL_DATE)
        .appendLiteral(' ')
        .append(DateTimeFormatter.ISO_LOCAL_TIME)
        .appendPattern(" zzz")
        .toFormatter(Locale.ENGLISH);

Thank you for the comments/solutions.

huangapple
  • 本文由 发表于 2023年2月24日 09:12:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定