【问题标题】:Date format changes using Joda API使用 Joda API 更改日期格式
【发布时间】:2015-10-01 06:59:43
【问题描述】:

我在我的代码中使用了 joda API,但我无法以正确的格式显示日期: 示例代码:

DateTimeZone gmtTimeZone = DateTimeZone.forID("GMT");
DateTimeZone pstTimeZone = DateTimeZone.forID("America/Chicago");                
DateTimeFormatter startEndFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").withZone(pstTimeZone);
DateTimeFormatter startEndOutFormat = DateTimeFormat.forPattern("dd-MM-yyyy HH:mm:ss").withZone(gmtTimeZone);
DateTimeFormatter batchFormat = DateTimeFormat.forPattern("dd-MMM-yyyy");
DateTimeFormatter durationFormat = DateTimeFormat.forPattern("hh:mm:ss");

在运行代码时,时间值会发生变化。 例如:

"30-09-2015 16:21:48"

应该是:

"30-09-2015 04:21:48"

【问题讨论】:

  • 您的代码创建了 4 种不同的格式,但根本不使用它们。发布一个重现问题的最小示例。告诉我们您希望该示例做什么,以及做什么。
  • 试试hh:mm:ss 而不是HH:mm:ss

标签: java api timezone jodatime


【解决方案1】:

你必须学习java数据格式化字符。请参阅我在SimpleDateFormat Documentation 找到的下表

所以, 将您的 dd-MM-yyyy HH:mm:ssdate 格式更改为 dd-MM-yyyy hh:mm:ss

【讨论】:

  • 不不不。 Joda-Time 使用一组不同的模式符号。和 SimpleDateFormat 中使用的不一样,
  • INCORRECT 正如上面的 Hochschild 评论所说,这个答案不正确。 Joda-Time 有自己的格式化模式。与 java.text.SimpleDateFormat 非常相似,但有些不同。如果您拥有本田,请勿阅读Yugo 维修手册。
猜你喜欢
  • 1970-01-01
  • 2017-01-11
  • 2013-12-04
  • 1970-01-01
  • 2012-11-04
  • 2016-04-16
  • 2014-05-07
  • 2018-03-11
  • 2015-05-12
相关资源
最近更新 更多