Mehrere Ausnahmen werden beim Analysieren der Datumszeichenfolge ausgelöst
Ich versuche zu Parsen einen string in ein Datum im Konstruktor ein Objekt, die wir rufen Example
. Hier ist der code
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);
}
}
Nun, ich bin die Erstellung dieser Objekte in einer Tomcat-Instanz (ob das einen Unterschied macht oder nicht).
Bekomme ich folgende Arten von Ausnahmen
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 scheitert es bei den Terminen
2012-07-06 18:57:31
2012-07-06 19:00:07
2012-07-06 19:13:21
2012-07-06 19:08:08
Jedoch, wenn ich einen unit-test erhalte ich folgende Werte für time
aus diesen strings
1341615451000
1341615607000
1341616401000
1341616088000
Also die SimpleDateFormat
Objekt funktioniert... aber nicht auf dem server? Ich habe bemerkt, dass dieses Problem tritt auf, in der Nähe der Start des Servers, und dann nicht später, wenn das hilft überhaupt. Nicht wirklich sicher, was als Nächstes zu tun ist.
Verwendung von Tomcat 7.0 und Java 1.6 update 32.
InformationsquelleAutor der Frage Craigy | 2012-07-06
Du musst angemeldet sein, um einen Kommentar abzugeben.
"=SimpleDateFormat ist nicht thread-sicher, so erklärt https://www.palantir.com/2007/07/simpledateformat-is-not-thread-safe/
Manchmal, wirklich seltsam oder nicht-logische Verhalten, wenn Sie mit "statischen" Objekten kommt von parallelitätsproblemen
Zur Behebung solcher Fall, verwenden Sie eine neue Instanz jedes mal (Es ist besser als die Synchronisierung, Synchronisierung erhöhen kann, ein Engpass Problem)
InformationsquelleAutor der Antwort cporte
"=SimpleDateFormat ist nicht thread-safe: finden Sie in der Javadoc.
InformationsquelleAutor der Antwort EJP
Als Java-8+, könnte es besser sein, lieber ein
java.time
- basierte Lösung.Klassen in
java.time
sind thread sicher.Können Sie mit gleichwertiger Datum der Extraktion so:
und dann die Abfrage der Felder, die Sie benötigen, aus
date
.Mehr details in der JavaDoc für DateTimeFormatter und LocalDateTime.
Anderen TLDR Ressource mit zwei ready-to-use Beispiele.
InformationsquelleAutor der Antwort jopasserat