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

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

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)

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

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

答案1

得分: 1

# 简要说明

```java
LocalDateTime
.parse( 
    "20230622233803466" , 
    DateTimeFormatter.ofPattern ( "uuuuMMddHHmmssSSS" ) 
)
.toString()
.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 相比,它缺少一个数字。

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

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

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

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

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

String iso8601 = ldt.toString() ; 

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

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

Ideone.com上运行此代码

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

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

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

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

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

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


<details>
<summary>英文:</summary>

# tl;dr

    LocalDateTime
    .parse( 
        &quot;20230622233803466&quot; , 
        DateTimeFormatter.ofPattern ( &quot;uuuuMMddHHmmssSSS&quot; ) 
    )
    .toString()
    .replace( &quot;T&quot; , &quot; &quot; )

&gt;2023-06-22 23:38:03.466

# Details

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.

# *java.time* classes

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`. 

Define a formatting pattern to match your input.

    String input = &quot;20230622233803466&quot;;
    DateTimeFormatter f = DateTimeFormatter.ofPattern ( &quot;uuuuMMddHHmmssSSS&quot; );

Parse as a `LocalDateTime`, a date with time of day but lacking the context of a time zone or offset-from-UTC.

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

Generate text in standard ISO 8601 format.

    String iso8601 = ldt.toString() ; 

Replace the `T` in the middle with a SPACE, per your desired output.

    String output = iso8601.replace( &quot;T&quot; , &quot; &quot; ) ;

See this code [run at Ideone.com][1].

&gt;iso8601 = 2023-06-22T23:38:03.466
&gt;
&gt;output = 2023-06-22 23:38:03.466


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.

    ZoneId z = ZoneId.of( &quot;Europe/London&quot; ) ;
    ZonedDateTime zdt = ldt.atZone( z ) ;

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


----------

Tip: Educate the publisher of your data about the benefits of using standard [ISO 8601][2] formats when communicating date-time values textually.


  [1]: https://ideone.com/UBK9uD
  [2]: https://en.wikipedia.org/wiki/ISO_8601

</details>



# 答案2
**得分**: 0

如果您有一个已知格式的字符串,您可以提取所需部分并构建任何接受这些部分的日期/时间对象,例如`ZonedDateTime`。

如果您需要一个`Date`对象,您可以基于`ZonedDateTime`来构建它。

```java
public static void main(String... args) {
    String str = "2023062223380346";
    ZonedDateTime zonedDateTime = convert(str, ZoneId.of("Europe/London"));
    System.out.println(zonedDateTime);  // 2023-06-22T23:38:03.000000046+01:00[Europe/London]
}

public static ZonedDateTime convert(String str, ZoneId zoneId) {
    return ZonedDateTime.of(Integer.parseInt(str.substring(0, 4)),
                            Integer.parseInt(str.substring(4, 6)),
                            Integer.parseInt(str.substring(6, 8)),
                            Integer.parseInt(str.substring(8, 10)),
                            Integer.parseInt(str.substring(10, 12)),
                            Integer.parseInt(str.substring(12, 14)),
                            Integer.parseInt(str.substring(14)),
                            zoneId);
}

请注意,上面的代码是用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.

public static void main(String... args) {
    String str = &quot;2023062223380346&quot;;
    ZonedDateTime zonedDateTime = convert(str, ZoneId.of(&quot;Europe/London&quot;));
    System.out.println(zonedDateTime);  // 2023-06-22T23:38:03.000000046+01:00[Europe/London]
}

public static ZonedDateTime convert(String str, ZoneId zoneId) {
    return ZonedDateTime.of(Integer.parseInt(str.substring(0, 4)),
                            Integer.parseInt(str.substring(4, 6)),
                            Integer.parseInt(str.substring(6, 8)),
                            Integer.parseInt(str.substring(8, 10)),
                            Integer.parseInt(str.substring(10, 12)),
                            Integer.parseInt(str.substring(12, 14)),
                            Integer.parseInt(str.substring(14)),
                            zoneId);
}

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:

确定