Java不可稀疏的日期异常 [英] Java Unparseable Date Exception
问题描述
我试图用正斜杠来替换连字符,但是它导致一个不可稀释的日期异常
I am trying to replace the hyphens with a forward slash, but it results to an unparseable date exception
String test = "2014-04-01 05:00:00";
Date date = new SimpleDateFormat("YYYY/MM/dd hh:mm:ss", Locale.ENGLISH).parse(test);
System.out.println(date);
我有必要的值被转换,有人可以告诉我为什么它返回错误?另外,我想在格式的末尾添加一个 am / pm标记
,这是可能的吗?
I have the necessary values for it to be converted, can someone tell me why it returns an error? Plus I wanted to append an am/pm marker
at the end of the format, is this possible?
推荐答案
您需要以正确的格式将 String
解析为 Date
作为输入 String
You need to parse String
to Date
first in correct format as input String
yyyy-MM-dd HH:mm:ss
然后您可以使用format()以其他格式打印
then you can use format() to print it in other format
yyyy/MM/dd hh:mm:ss
并且不要指望 toString()
方法 Date
类返回格式化的值,它是固定的实现
and don't expect toString()
method of Date
class return formatted value, it is fixed implementation
这篇关于Java不可稀疏的日期异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!