Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleDateFormat.parse returns errorIndex as 0

Using jdk1.7.0_55, I'm getting parseposition.errorIndex as 0 in case of timezone is not standard one.

But using jdk1.6.0_38 I'm getting parseposition.errorIndex as 21 for the same wrong timezone format

Below is the sample:

String date = "13 Jan 2005 21:45:34 ABC";
String format = "dd MMM yyyy HH:mm:ss z";
ParsePosition pp = new ParsePosition(0);
pp.setIndex(0);
SimpleDateFormat sd = new SimpleDateFormat(format,Locale.ENGLISH);
Date d = sd.parse(date,pp);
System.out.println("Error Index "+pp.getErrorIndex());

Result Using jdk 6:

Error Index 21

Result Using jdk 7

Error Index 0

Why there is difference in it?

Please help

like image 496
Roshan Avatar asked May 06 '15 12:05

Roshan


People also ask

What is simpledateformat parse method in Java?

SimpleDateFormat parse() Method in Java with Examples. The parse() Method of SimpleDateFormat class is used to parse the text from a string to produce the Date. The method parses the text starting at the index given by a start position.

What is simpledateformat in C++?

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date → text), parsing (text → date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.

How to parse the text from a string to produce date?

The parse () Method of SimpleDateFormat class is used to parse the text from a string to produce the Date. The method parses the text starting at the index given by a start position. Parameters: The method takes two parameters: Attention reader! Don’t stop learning now.

How does simpledateformat interpret the abbreviated year pattern?

For parsing with the abbreviated year pattern ("y" or "yy"), SimpleDateFormat must interpret the abbreviated year relative to some century. It does this by adjusting dates to be within 80 years before and 20 years after the time the SimpleDateFormat instance is created.


1 Answers

Indeed this is a regression. Still broken in latest Java 8 build. Public OpenJDK bug tracker does not list it, so I have reported it to Oracle for review. It is tracked by Oracle with Review ID: JI-9021556

Unfortunately there is no way for you to fix that.

like image 109
Fabian Lange Avatar answered Nov 15 '22 01:11

Fabian Lange