JDK-6205670 : [Fmt-Da] java.text.SimpleDateFormat parse not correct
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 1.4.2
  • Priority: P3
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2004-12-08
  • Updated: 2010-07-29
  • Resolved: 2005-08-17
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
eclipse 3.0

C:\Dokumente und Einstellungen\werner>java -version
java version "1.4.2_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Windows 2000

A DESCRIPTION OF THE PROBLEM :
 *     ---- SimpleDateFormat ----
 *
 * I've parsed the same string under different formats.
 * Here are the results:
 *
 * 1. is very incompatible
 * --------------------------------------------------
 * Date-String to parse:   1999-12-05T09:08:07+0305
 * Format for parsing:     yyyyMMdd
 * Result:                 1998-11-02  00:00:00.000 +0100
 * ---------- but error should occur ----------------
 *
 * 2. an appended time zone does'nt detect
 * --------------------------------------------------
 * Date-String to parse:   1999-12-05T09:08:07+0305
 * Format for parsing:     yyyy-MM-dd'T'HH:mm:ss
 * Result:                 1999-12-05  09:08:07.000 +0100
 * ---------- but error should occur ----------------
 *



REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
/*
 * Erzeugt: 07.12.2004
 * Author: peter werner
 *
 * Security Engineering
 * Business Unit ITC Security
 * T-Systems-International
 * Goslarer Ufer 35, 10589 Berlin
 *
 * ###@###.###
 *
 * This Java program demontrates 2 errors in Class
 *     ---- SimpleDateFormat ----
 *
 * I've parsed the same string under different formats.
 * Here are the results:
 *
 * 1. is very incompatible
 * --------------------------------------------------
 * Date-String to parse:   1999-12-05T09:08:07+0305
 * Format for parsing:     yyyyMMdd
 * Result:                 1998-11-02  00:00:00.000 +0100
 * ---------- but error should occur ----------------
 *
 * 2. an appended time zone does'nt detect
 * --------------------------------------------------
 * Date-String to parse:   1999-12-05T09:08:07+0305
 * Format for parsing:     yyyy-MM-dd'T'HH:mm:ss
 * Result:                 1999-12-05  09:08:07.000 +0100
 * ---------- but error should occur ----------------
 *
 */
package test;

import java.text.FieldPosition;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author werner
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class TestDateTime1 {
	String format = "yyyyMMdd";               //bug 1
	String date = "1999-12-05T09:08:07+0305"; //bug 1
	
	//switch comments to show bug 2
	//String format = "yyyy-MM-dd'T'HH:mm:ss";  //bug 2
	//String date = "1999-12-05T09:08:07+0305"; //bug 2
	
	
	public static void main(String[] args) {
		TestDateTime1 tdt = new TestDateTime1();
		tdt.xxx();
	}

	public void xxx() {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			Date d = sdf.parse(date);
			System.getProperty().
			System.out.println("--------------------------------------------------");
			System.out.println("Date-String to parse:   "+date);
			System.out.println("Format for parsing:     "+format);
			System.out.println("Result:                 "+toString(d)+"\n");
			System.out.println("---------- but error should occur ----------------");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String toString(Date d) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss.SSS Z");
		StringBuffer sb = new StringBuffer("");
		sdf.format(d, sb, new FieldPosition(0));
		return sb.substring(0);
	}
}

---------- END SOURCE ----------
###@###.### 2004-12-08 09:41:24 GMT

Comments
EVALUATION Case 1. * -------------------------------------------------- * Date-String to parse: 1999-12-05T09:08:07+0305 * Format for parsing: yyyyMMdd * Result: 1998-11-02 00:00:00.000 +0100 * ---------- but error should occur ---------------- If the submitter wants "1999-12-05" to be treated as December 5th of 1999, he/she should use "yyyy-MM-dd". Otherwise, if he/she really wants it to be treated as "minus December and minus 5th of 1999", this is a bug. MM and dd are fixed-length text. And, '-' has been included in the length of month. In the above exmple, "-1" is treated as month, "2" is treated as date. (In this case, '-' after '2' works as a delimiter of fields.) The minus sign should not be included in the length of number. But is this really important in real-world applications?? Case 2. * -------------------------------------------------- * Date-String to parse: 1999-12-05T09:08:07+0305 * Format for parsing: yyyy-MM-dd'T'HH:mm:ss * Result: 1999-12-05 09:08:07.000 +0100 * ---------- but error should occur ---------------- There are two problems in the example in Description. 1. The given pattern doesn't include 'Z' for time zone. Therefore, it is natural that the given time zone "+0305" isn't recognized at all. 2. Because Date class doesn't include timezone information, format() method in another DateFormat instance cannot know the original time zone from the given date and uses the default time zone of the user's runtime environment. But even if 'Z' is used in the pattern and format() of the same DateFormat() instance as parse() is used, the given time zone cannot be used in formatted String. There's no way to extract timezone information from the parsed String for reusing now due to bug 4705403. *** (#1 of 1): [ UNSAVED ] ###@###.### I decided to close this bug as "will not fix" because Case1 doesn't seem to be a problem in real-world apps. Users can use parse(String, ParsePosition) in order to know the index of parsed(or not-parsed) characters and throw ParseException if necessary.
16-08-2005