我有一个大整数值,需要将其转换为日期时间格式 (yyyy-mm-dd hr:ss:mss)。

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

I have a big integer value which I need to convert into date time in (yyyy-mm-dd hr:ss:mss)

问题

Expected output- 2023-06-22 23:38:03:466

英文:

I have a big integer value which I need to convert into date time in (yyyy-mm-dd hr:ss:mss)

  1. BigInteger sum= new BigInteger("2023062223380346");
  2. // long unixSeconds = 1429582984839;
  3. Date date1 = new Date(sum);
  4. SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a");
  5. sdf.setTimeZone(TimeZone.getTimeZone("GMT+1"));
  6. String formattedDate = sdf.format(date1);
  7. System.out.println(formattedDate);

Expected output- 2023-06-22 23:38:03:466

答案1

得分: 1

  1. # 简要说明
  2. ```java
  3. LocalDateTime
  4. .parse(
  5. "20230622233803466" ,
  6. DateTimeFormatter.ofPattern ( "uuuuMMddHHmmssSSS" )
  7. )
  8. .toString()
  9. .replace( "T" , " " )

2023-06-22 23:38:03.466

详细信息

您正在使用可怕的传统日期时间类,这些类在多年前被现代的java.time类(JSR 310中定义)所取代。始终使用java.time类来处理日期时间。

java.time

我假设您的示例输入字符串 "2023062223380346" 是一个打字错误,因为与您期望的输出 2023-06-22 23:38:03:466 相比,它缺少一个数字。

定义一个格式化模式以匹配您的输入。

  1. String input = "20230622233803466";
  2. DateTimeFormatter f = DateTimeFormatter.ofPattern ( "uuuuMMddHHmmssSSS" );

将其解析为LocalDateTime,一个带有日期和时间的日期,但缺少时区或UTC偏移量的上下文。

  1. LocalDateTime ldt = LocalDateTime.parse( input , f ) ;

生成标准ISO 8601格式的文本。

  1. String iso8601 = ldt.toString() ;

将中间的 T 替换为空格,与您的期望输出相匹配。

  1. String output = iso8601.replace( "T" , " " ) ;

Ideone.com上运行此代码

iso8601 = 2023-06-22T23:38:03.466

output = 2023-06-22 23:38:03.466

显然,您希望将其解释为表示特定时区的时刻。使用时区名称而不是假设偏移量。偏移量可以在不同时间段内变化 - 这就是时区的定义,即由特定地区的人民根据他们的政治决策而决定的,过去、现在和将来的偏移量变化的命名历史。

  1. ZoneId z = ZoneId.of( "Europe/London" ) ;
  2. ZonedDateTime zdt = ldt.atZone( z ) ;

zdt.toString() = 2023-06-22T23:38:03.466+01:00[Europe/London]

提示:向数据的发布者传达在以文本形式传递日期时间值时使用标准ISO 8601格式的好处。

  1. <details>
  2. <summary>英文:</summary>
  3. # tl;dr
  4. LocalDateTime
  5. .parse(
  6. &quot;20230622233803466&quot; ,
  7. DateTimeFormatter.ofPattern ( &quot;uuuuMMddHHmmssSSS&quot; )
  8. )
  9. .toString()
  10. .replace( &quot;T&quot; , &quot; &quot; )
  11. &gt;2023-06-22 23:38:03.466
  12. # Details
  13. You are using terrible legacy date-time classes were years ago supplanted by the modern java.time classes defined in JSR 310. Always use *java.time* classes for your date-time handling.
  14. # *java.time* classes
  15. I assume your example input string `&quot;2023062223380346&quot;` is a typo, as it is missing a digit on the end when compared to your desired output `2023-06-22 23:38:03:466`.
  16. Define a formatting pattern to match your input.
  17. String input = &quot;20230622233803466&quot;;
  18. DateTimeFormatter f = DateTimeFormatter.ofPattern ( &quot;uuuuMMddHHmmssSSS&quot; );
  19. Parse as a `LocalDateTime`, a date with time of day but lacking the context of a time zone or offset-from-UTC.
  20. LocalDateTime ldt = LocalDateTime.parse( input , f ) ;
  21. Generate text in standard ISO 8601 format.
  22. String iso8601 = ldt.toString() ;
  23. Replace the `T` in the middle with a SPACE, per your desired output.
  24. String output = iso8601.replace( &quot;T&quot; , &quot; &quot; ) ;
  25. See this code [run at Ideone.com][1].
  26. &gt;iso8601 = 2023-06-22T23:38:03.466
  27. &gt;
  28. &gt;output = 2023-06-22 23:38:03.466
  29. Apparently you want to interpret this as representing a moment in a specific time zone. Use time zone names rather than assuming an offset. Offsets can vary for different periods of time — that is the definition of a time zone, a named history of the past, present, and future changes to the offset used by the people of a particular region as decided by their politicians.
  30. ZoneId z = ZoneId.of( &quot;Europe/London&quot; ) ;
  31. ZonedDateTime zdt = ldt.atZone( z ) ;
  32. &gt;zdt.toString() = 2023-06-22T23:38:03.466+01:00[Europe/London]
  33. ----------
  34. Tip: Educate the publisher of your data about the benefits of using standard [ISO 8601][2] formats when communicating date-time values textually.
  35. [1]: https://ideone.com/UBK9uD
  36. [2]: https://en.wikipedia.org/wiki/ISO_8601
  37. </details>
  38. # 答案2
  39. **得分**: 0
  40. 如果您有一个已知格式的字符串,您可以提取所需部分并构建任何接受这些部分的日期/时间对象,例如`ZonedDateTime`。
  41. 如果您需要一个`Date`对象,您可以基于`ZonedDateTime`来构建它。
  42. ```java
  43. public static void main(String... args) {
  44. String str = "2023062223380346";
  45. ZonedDateTime zonedDateTime = convert(str, ZoneId.of("Europe/London"));
  46. System.out.println(zonedDateTime); // 2023-06-22T23:38:03.000000046+01:00[Europe/London]
  47. }
  48. public static ZonedDateTime convert(String str, ZoneId zoneId) {
  49. return ZonedDateTime.of(Integer.parseInt(str.substring(0, 4)),
  50. Integer.parseInt(str.substring(4, 6)),
  51. Integer.parseInt(str.substring(6, 8)),
  52. Integer.parseInt(str.substring(8, 10)),
  53. Integer.parseInt(str.substring(10, 12)),
  54. Integer.parseInt(str.substring(12, 14)),
  55. Integer.parseInt(str.substring(14)),
  56. zoneId);
  57. }

请注意,上面的代码是用Java编写的,用于从给定字符串构建ZonedDateTime对象。

英文:

In case you get a strign with known format, you can extract required part and build any date/time object, that accept all these parts separately. E.g. ZonedDateTime.

In case you nee a Date object, you can build it based on ZonedDateTime.

  1. public static void main(String... args) {
  2. String str = &quot;2023062223380346&quot;;
  3. ZonedDateTime zonedDateTime = convert(str, ZoneId.of(&quot;Europe/London&quot;));
  4. System.out.println(zonedDateTime); // 2023-06-22T23:38:03.000000046+01:00[Europe/London]
  5. }
  6. public static ZonedDateTime convert(String str, ZoneId zoneId) {
  7. return ZonedDateTime.of(Integer.parseInt(str.substring(0, 4)),
  8. Integer.parseInt(str.substring(4, 6)),
  9. Integer.parseInt(str.substring(6, 8)),
  10. Integer.parseInt(str.substring(8, 10)),
  11. Integer.parseInt(str.substring(10, 12)),
  12. Integer.parseInt(str.substring(12, 14)),
  13. Integer.parseInt(str.substring(14)),
  14. zoneId);
  15. }

huangapple
  • 本文由 发表于 2023年7月7日 02:44:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631716.html
匿名

发表评论

匿名网友

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

确定