java 解析日期字符串抛出多个异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11368632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple exceptions thrown parsing date string
提问by Craigy
I am trying to parse a string into a date in the constructor of an object, which we will call Example
. Here is the code
我试图在对象的构造函数中将字符串解析为日期,我们将调用它Example
。这是代码
private static final SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
private long time;
public Example(String date) {
try {
this.time = sdf.parse(date).getTime();
} catch (Exception e) {
logger.log(Level.WARNING, "Exception while parsing date " + date, e);
}
}
Now, I am creating these objects in a Tomcat instance (whether that makes any difference or not).
现在,我在 Tomcat 实例中创建这些对象(无论是否有所不同)。
I get the following types of exceptions
我得到以下类型的异常
Fri Jul 06 15:13:48 EDT 2012 WARNING: Exception while parsing date 2012-07-06 18:57:31
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:1297)
at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1589)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1311)
at java.text.DateFormat.parse(DateFormat.java:335)
at ...
Fri Jul 06 15:13:48 EDT 2012 WARNING: Exception while parsing date 2012-07-06 19:00:07
java.lang.NumberFormatException: multiple points
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1082)
at java.lang.Double.parseDouble(Double.java:510)
at java.text.DigitList.getDouble(DigitList.java:151)
at java.text.DecimalFormat.parse(DecimalFormat.java:1302)
at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1934)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1311)
at java.text.DateFormat.parse(DateFormat.java:335)
at ...
Fri Jul 06 15:13:48 EDT 2012 WARNING: Exception while parsing date 2012-07-06 19:13:21
java.lang.ArrayIndexOutOfBoundsException: -1
at java.text.DigitList.fitsIntoLong(DigitList.java:212)
at java.text.DecimalFormat.parse(DecimalFormat.java:1295)
at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1934)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1311)
at java.text.DateFormat.parse(DateFormat.java:335)
at ...
Fri Jul 06 15:48:06 EDT 2012 WARNING: Exception while parsing last check string 2012-07-06 19:08:08
java.lang.NumberFormatException: For input string: ".200172E4.200172"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222)
at java.lang.Double.parseDouble(Double.java:510)
at java.text.DigitList.getDouble(DigitList.java:151)
at java.text.DecimalFormat.parse(DecimalFormat.java:1302)
at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1589)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1311)
at java.text.DateFormat.parse(DateFormat.java:335)
at ...
So it fails for the dates
所以它的日期失败
2012-07-06 18:57:31
2012-07-06 19:00:07
2012-07-06 19:13:21
2012-07-06 19:08:08
However, if I make a unit test I get the following values for time
from these strings
但是,如果我进行单元测试,我会time
从这些字符串中获得以下值
1341615451000
1341615607000
1341616401000
1341616088000
So the SimpleDateFormat
object does work... but not on the server? I have noticed that this issue occurs near the startup of the server, and then not later on, if that helps at all. Not really sure what to do next.
所以该SimpleDateFormat
对象确实有效...但不在服务器上?我注意到这个问题发生在服务器启动附近,然后不会发生,如果这有帮助的话。不太确定接下来要做什么。
Using Tomcat 7.0 and Java 1.6 update 32.
使用 Tomcat 7.0 和 Java 1.6 更新 32。
回答by cporte
SimpleDateFormat is not thread safe, as explained https://www.palantir.com/2007/07/simpledateformat-is-not-thread-safe/Sometimes, really strange or non-logical behaviors when using "static" objects comes from concurrency issues
SimpleDateFormat 不是线程安全的,正如https://www.palantir.com/2007/07/simpledateformat-is-not-thread-safe/有时,使用“静态”对象时的真正奇怪或非逻辑行为来自并发问题
To resolve such a case, use a new instance each time (It is better than synchronizing, as synchronization can raise a bottleneck issue)
要解决这种情况,每次都使用一个新实例(这比同步要好,因为同步会引起瓶颈问题)
回答by user207421
SimpleDateFormat is not thread-safe: see the Javadoc.
SimpleDateFormat 不是线程安全的:请参阅 Javadoc。
回答by jopasserat
As of Java 8+, it might be better to prefer a java.time
based solution.
从 Java 8+ 开始,最好选择java.time
基于解决方案的解决方案。
The classes in java.time
are thread safe.
中的类java.time
是线程安全的。
You can achieve the equivalent date extraction like that:
您可以像这样实现等效的日期提取:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
...
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime date = LocalDateTime.parse(str, dockerDateFormatter);
and then query the fields you need from date
.
然后从 中查询您需要的字段date
。
More details in the JavaDoc for DateTimeFormatterand LocalDateTime.
JavaDoc 中有关DateTimeFormatter和LocalDateTime 的更多详细信息。
Another TLDR resourcewith two ready-to-use examples.
另一个TLDR 资源,包含两个现成的示例。