【发布时间】:2017-09-11 11:29:05
【问题描述】:
我使用 SimpleDateFormat 将 Date 格式化为 String 不带符号 :,/,-,但它格式很奇怪。
这是我的代码:
public static String getFormatedDate(Date date, String format) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.format(date);
}
我这样称呼它:
DateUtil.getFormatedDate(new Date(), "DDMMYYYYHHMMSS")
回电不正确2540920170909379应该是这样11092017093405 = 11/09/2017 09:34:05 p>
【问题讨论】:
-
观察那些格式模式字母的大小写。
d和D的意思不同。y和Y等都不是。 -
告诉我们,你为什么还坚持使用过时的
SimpleDateFormat类?从 Stack Overflow 上的问题数量来看,这门课似乎引起了很多混乱和麻烦。我建议你不要扔掉,而是使用the modern Java date and time API。与它一起工作要好得多。它的格式化类是DateTimeFormatter。 -
如果可能,请使用标准 ISO 8601 格式,而不是自己设计。
-
ZonedDateTime.now().format( DateTimeFormatter.ofPattern( "ddMMuuuuHHmmss" ) )
标签: java date simpledateformat