gpt4 book ai didi

java - SimpleDateFormat:解析后小时为HH

转载 作者:行者123 更新时间:2023-11-30 03:21:46 26 4
gpt4 key购买 nike

我有一个 String,它是 2013-8-25, 16:33,现在我需要将它转换成 Date。为此,我使用:

Date mDate = new SimpleDateFormat("yyyy-MM-dd, HH:mm").parse("2013-8-25, 16:33");

然后我需要得到一个正确模式的String,这是

private static final String DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

这是我的做法:

  public static String convertDateToString(Date date) {
String string = DateFormat.format(DATE_PATTERN, date).toString();
Logger.logVerbose(TAG, "Date converted into String is " + string);
return string;
}

但是:

Date converted into String is 2013-08-25THH:33:00.SSSZ

为什么小时仍然是 HH?谢谢!

最佳答案

我强烈怀疑您遇到了与 DateFormat 的兼容性问题.特别是,从文档中:

The format methods in this class implement a subset of Unicode UTS #35 patterns. The subset currently supported by this class includes the following format characters: acdEHhLKkLMmsyz. Up to API level 17, only adEhkMmszy were supported. Note that this class incorrectly implements k as if it were H for backwards compatibility.

我建议您使用 SimpleDateFormat 进行格式化,以避免出现此问题。

关于java - SimpleDateFormat:解析后小时为HH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19006593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com