Java SimpleDataFormatter引发ParseException
我有这两个方法,是现在写的。按照这个场景,当我从数据库中获取数据字段时,它是BigDecimal格式的。所以我决定为它编写一个测试(formatDate()方法)。我将BigDecimal传递给该方法,但似乎我编写了一些错误的代码。从我在示例和SimpleDataFormatAPI中看到的情况来看,我认为我已经正确地编写了代码,但我似乎无法找出让它抛出Parsex的错误。有人能告诉我发生了什么事吗Java SimpleDataFormatter引发ParseException,java,simpledateformat,parseexception,Java,Simpledateformat,Parseexception,我有这两个方法,是现在写的。按照这个场景,当我从数据库中获取数据字段时,它是BigDecimal格式的。所以我决定为它编写一个测试(formatDate()方法)。我将BigDecimal传递给该方法,但似乎我编写了一些错误的代码。从我在示例和SimpleDataFormatAPI中看到的情况来看,我认为我已经正确地编写了代码,但我似乎无法找出让它抛出Parsex的错误。有人能告诉我发生了什么事吗 private void loadMap() { //TODO: Uncomment w
private void loadMap() {
//TODO: Uncomment when finished testing.
//DO OTHER STUFF
BigDecimal bd = new BigDecimal(12051998);
System.out.println(formatDate(bd));
}
private String formatDate(BigDecimal bd) {
String value = bd.toString();
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
try {
return format.parse(value);
} catch (ParseException pEx) {
logger.error(pEx);
return "Bad Date Format";
}
}
提前感谢,致以最热烈的问候
- 乔希
SimpleDateFormat format = new SimpleDateFormat("MMddyyyy");
返回格式.parse(值)代码>/值应为日期类型,而不是字符串
尝试将日期格式从MMddyyyy更改为MM/dd/yyyy:这对我来说很好
public static void main(String[] args) throws ParseException {
BigDecimal bd = new BigDecimal(12051998);
String s = bd.toString();
System.out.println(s);
DateFormat originalFormat = new SimpleDateFormat("MMddyyyy");
DateFormat targetFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = originalFormat.parse(s);
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);
}
输出:
12051998
12/05/1998
应该是
SimpleDateFormat format = new SimpleDateFormat("MMddyyyy");
返回格式.parse(值)代码>/值应为日期类型,而不是字符串
尝试将日期格式从MMddyyyy更改为MM/dd/yyyy:这对我来说很好
public static void main(String[] args) throws ParseException {
BigDecimal bd = new BigDecimal(12051998);
String s = bd.toString();
System.out.println(s);
DateFormat originalFormat = new SimpleDateFormat("MMddyyyy");
DateFormat targetFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = originalFormat.parse(s);
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);
}
输出:
12051998
12/05/1998
BigDecimal
对于保存日期值来说是一个奇怪的选择。@GriffeyDog它是在AS/400上用于DB2的IBM/Genelco软件。一切都很奇怪。答案是好的,但是你可能也想考虑使用<代码> BigDecimal < /C>是一个奇怪的选择,用于保存日期值。@ GriffeyDog是IBM /GeelCo软件的DB2上的As/400。一切都很奇怪。答案是好的,但是你可能也想考虑我已经更新了MMDYYYY的方法。我得到的消息是:不可解析的日期:“12051998”@ JoshGooding。你想把日期从MMddyyyy转换成MM/DD/YYYY格式吗?是的,我想把12051998转换成12/05/1998。成功了。我不知道那天我基本上要做双重转换。我试着一步到位,但没有意识到。似乎你不可能一步到位。非常感谢。我已将该方法更新为mmddyyy。我收到的消息是:不可解析日期:“12051998”@JoshGooding您想将日期格式从mmddyyy更改为MM/dd/yyyy吗?是的,我想将12051998转换为1998年5月12日。成功了。我不知道那天我基本上要做双重转换。我试着一步到位,但没有意识到。似乎你不可能一步到位。非常感谢。