简体   繁体   English

在Java中解析日期时的异常

[英]Exception when Parsing Dates in Java

Edited in accordance with comments 按照评论编辑

For some reason, I seem to be getting this exception randomly occurring when I am trying to parse between Date and String formats in Java. 出于某种原因,当我尝试在Java中解析DateString格式之间时,我似乎正在随机发生此异常。

Here is a snippet of the code I've got (this is just for testing purposes): 这是我得到的代码片段(这仅用于测试目的):

SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
for(...){
  ...
   System.out.println("Before parsing: [" + lastEntryDate + "]");
   Date date = formatter.parse(lastEntryDate);
   System.out.println("After parsing: [" + date + "]”);
}

And the output: 并输出:

Before parsing: [Sun Aug 07 22:45:30 EST 2011]
After parsing: [Sun Aug 07 22:45:30 EST 2011]
Before parsing: [Sun Aug 07 22:45:31 EST 2011]
After parsing: [Sun Aug 07 22:45:31 EST 2011]
Before parsing: [Sun Aug 07 22:45:31 EST 2011]
After parsing: [Sun Aug 07 22:45:31 EST 2011]
Before parsing: [Sun Aug 07 22:45:31 EST 2011]
After parsing: [Sun Aug 07 22:45:31 EST 2011]
Before parsing: [Sun Aug 07 22:45:31 EST 2011]
After parsing: [Sun Aug 07 22:45:31 EST 2011]
Before parsing: [Sun Aug 07 22:45:31 EST 2011]
java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Long.parseLong(Long.java:431)
    at java.lang.Long.parseLong(Long.java:468)
    at java.text.DigitList.getLong(DigitList.java:177)
    at java.text.DecimalFormat.parse(DecimalFormat.java:1298)
    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1936)
    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1312)
    at java.text.DateFormat.parse(DateFormat.java:335)
    at proj01.servicebus.ServiceBus.writeFeedEntry(ServiceBus.java:211)
    at proj01.servicebus.ServiceBus.writeFeedEntries(ServiceBus.java:243)
    at proj01.servicebus.ServiceBus.access$1(ServiceBus.java:241)
    at proj01.servicebus.ServiceBus$1.call(ServiceBus.java:84)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)

The problem is: 问题是:

  • sometimes, the parsing fails ?! 有时,解析失败了?! (see for example, where it gives 00:00:00) (例如,见00:00:00)
  • sometimes, the exception shown is thrown ?! 有时,显示的异常被抛出?!
  • the problem seems to be random! 问题似乎是随机的!

What is the likely cause and solution to this problem? 这个问题的可能原因和解决方案是什么?

It looks like you're trying to parse an empty string. 看起来你正试图解析一个空字符串。 Therefore the error is in the input data - unfortunately, in that case you won't display the bad data, as you're only calling System.out.println once, after parsing. 因此错误在输入数据中 - 遗憾的是,在这种情况下,您将不会显示错误数据,因为您解析只调用一次System.out.println

If you change your diagnostics to: 如果将诊断更改为:

System.out.println("Before parsing: [" + lastEntryDate + "]");
Date date = formatter.parse(lastEntryDate);
System.out.println("After parsing: [" + date + "]");

I suspect you'll see that you get an exception when the data is missing or completely invalid; 我怀疑当数据丢失或完全无效时,您会看到异常; where it's parsing to "Sun Aug 07 00:00:00 EST 2011" I suspect you'll find that the input data really does have that information. 它解析为“Sun Aug 07 00:00:00 EST 2011”我怀疑你会发现输入数据真的有这些信息。

EDIT: If you're using multiple threads, you should not be sharing a SimpleDateFormat between the threads. 编辑:如果您使用多线程,你应该共享SimpleDateFormat线程之间。 Either create one in each thread or (my preference) use Joda Time instead - it's a much better library in general, and its formatters are thread-safe. 在每个线程中创建一个或者(我的偏好)使用Joda Time代替 - 它通常是一个更好的库,并且它的格式化程序线程安全的。

A failure that occurs "randomly" for the same input is strongly suggestive of a threading issue. 对于相同输入“随机”发生的故障强烈暗示了线程问题。 And the stacktrace tells me that you are most likely using multiple threads in this test. stacktrace告诉我你很可能在这个测试中使用多个线程。

I suspect the problem is that you are sharing one SimpleDateFormat object between multiple threads without properly synchronizing its use. 我怀疑问题是你在多个线程之间共享一个SimpleDateFormat对象而没有正确地同步它的使用。 The javadoc for SimpleDateFormat says this: SimpleDateFormatjavadoc说:

"Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally." “日期格式不同步。建议为每个线程创建单独的格式实例。如果多个线程同时访问格式,则必须在外部同步。” .

Would you mind to output stuff in two tiems so that we can actually always see what your trying to parse even if parsing fails: 您是否介意以两个组合输出内容,以便即使解析失败,我们也可以始终看到您尝试解析的内容:

System.out.print("["+lastEntryDate );
System.out.println("||"+formatter.parse(lastEntryDate)+"]”);`

Actually we can't see the input data that make parsing fail. 实际上我们无法看到使解析失败的输入数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM