【问题标题】:Java Convert dateTime to the given formatJava 将 dateTime 转换为给定的格式
【发布时间】:2020-09-05 04:01:24
【问题描述】:

我需要一些帮助来将给定的本地日期时间值(或实用日期)转换为具有(ASIA/COLOMBO)时区的以下格式之一。以下是格式(来自单信号 API)。

“2015 年 9 月 24 日星期四 14:00:00 GMT-0700 (PDT)”

“2015 年 9 月 24 日下午 2:00:00 UTC-07:00”

“2015-09-24 14:00:00 GMT-0700”

“2015 年 9 月 24 日 14:00:00 GMT-0700”

“2015 年 9 月 24 日星期四 14:00:00 GMT-0700(太平洋夏令时间)”

谢谢。

【问题讨论】:

  • 如何给出LocalDateTIme?是java.time.LocalDateTime 还是来自joda time 的?你能展示你自己的尝试吗?也许,你已经很接近让它工作了......
  • 从数据库中获取,java.time

标签: java timezone datetime-format java.util.date localdatetime


【解决方案1】:

ZonedDateTime

将日期、时间和区域实例化为 ZonedDateTime 对象。

LocalDate ld = LocalDate.of( 2015 , Month.SEPTEMBER , 24 ) ;
LocalTime lt = LocalTime.of( 14 , 0 ) ;
ZoneId z = ZoneId.of( "Asia/Colombo" ) ;
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;

如果给定 java.util.Date 对象,则转换为 Instant

Instant instant = myJavaUtilDate.toInstant() ;

应用区域以从 UTC 移动。

ZoneId z = ZoneId.of( "Asia/Colombo" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

要生成所需格式的文本,请使用DateTimeFormatter 类。这个问题已经在 Stack Overflow 上解决了很多次。因此,搜索该类名称以了解更多信息。

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd HH:mm:ss OOOO" ) ;
String output = zdt.format( f ) ;

2015-09-24 14:00:00 GMT+05:30

ISO 8601

您展示的那些格式都是糟糕的选择。将日期时间值交换为文本时,请使用标准 ISO 8601 格式。标准格式旨在易于机器解析,并且易于跨文化的人类阅读。

java.time 类在解析/生成字符串时默认使用 ISO 8601 格式。

【讨论】:

  • 感谢您的回复,我会检查一下,如果您能给我一个给定的模式会更好。
  • @TharinduEranga 有一个适合你。研究DateTimeFormatter Javadoc 以了解格式化模式代码。更好的是:向您的数据发布者宣传 ISO 8601。
【解决方案2】:

java.time 类是在 1.8 中引入的,它允许更灵活和简洁的代码。

Instant now = Instant.now();
ZonedDateTime dateTime = now.atZone(ZoneId.of("Asia/Colombo"));
DateTimeFormatter format = DateTimeFormatter.ofPattern("E M d u HH:mm:ss OOOO (z)");

// Using a format to convert at Temporal to a string
String formattedDate = format.format(dateTime);

// Using a format to convert a string to a Instant
Instant parsedDate = Instant.from(format.parse(formattedDate));

您可以使用DateTimeFormatter.ofPattern 或 DateTimeFormatter 中定义的众多常量之一来指定日期格式。 pattern I created 匹配您的第一个示例。

旧方法

这些类还没有被弃用,我发现对于简单的情况比现代方法更直接。

java.text.SimpleDateFormat;它可以处理往返于 String 和 java.util.Date 对象。

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss Z (z)");
format.setTimeZone(TimeZone.getTimeZone("Asia/Colombo"));
// Note that the current date and time will be used when instantiating a new Date object.
String formattedDate = format.format(new Date());

try {
    Date parsedDate = format.parse(formattedDate);
} catch (ParseException exception) {
    // Handle malformed date
}

您只需要create the pattern 就可以使用任何您想使用的格式。

EEE MMM dd yyyy HH:mm:ss Z (z) = Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)

【讨论】:

  • 那些糟糕的日期时间类在几年前被 JSR 310 中定义的现代 java,time 类所取代。建议在 2020 年使用它们是糟糕的建议。
  • 感谢您的回复,您能否为我提供一个新的解决方案,正如@BasilBourque 提到的那样,如果这些都相当旧。
  • 快到了。在我的语言环境中,我得到了tor. 5 21 2020 00:50:05 GMT+05:30 (IST)。您想为格式化程序提供一个语言环境,并且您需要 MMM 来表示月份的缩写。 'GMT'xx 会给你GMT+0530 没有冒号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-08
  • 2012-10-17
  • 1970-01-01
  • 2015-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多