JDK-4396385 : [Fmt-Da] SimpleDateFormat too lenient when parsing 1-based hours
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 1.2.2,1.3.0,1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_nt,windows_2000
  • CPU: x86
  • Submitted: 2000-12-08
  • Updated: 2010-12-21
  • Resolved: 2011-03-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b123Fixed
Description

Name: boT120536			Date: 12/08/2000


java version "1.2.2"
Classic VM (build JDK-1.2.2-W, native threads, symcjit)

In java documentation of SimpleDateFormat it is specified that the
symbol/pattern 'h' takes the value from 1 to 12 hour in am/pm. But actually it
takes values from 0-12 hours. Infact it should give an error while calling
parse function when the hour is specified as 0.

Here is the Sample code which demonstrates the bug

import java.text.*;
import java.util.*;

public class TestForBugInSimpleDateFormat
{
	public static void main(String arg[])
	{
		//This is valid value for format "hh:mmaa"
		String strValidTime = "12:30AM";
		
		/*This is not a valid value for format "hh:mmaa" as h
                  take value from 1-12 and 0 is invalid value*/
		String strInValidTime = "00:30AM";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
	
		simpleDateFormat.applyPattern("hh:mmaa");
		simpleDateFormat.setLenient(false);
		try
		{
			Date date1 = simpleDateFormat.parse(strValidTime);
			System.out.println("Date1 = " + date1);
			
		//Actually here it should give parse exception but it doesn't
			Date date2 = simpleDateFormat.parse(strInValidTime);
			System.out.println("Date2 = " + date2);
		}
		catch(Exception exception)
		{
			exception.printStackTrace();
		}
		
	}
}
(Review ID: 113487) 
======================================================================

Name: nt126004			Date: 09/11/2002


FULL PRODUCT VERSION :
J2SE 1.4.0-b92

FULL OPERATING SYSTEM VERSION : WINDOWS NT 4.0 WORSTATION

A DESCRIPTION OF THE PROBLEM :
Using the defined time patterns "h" (1-12) and "k" (1-24),
one should not be able to parse "00:01 AM" without an error
or the error index in the ParsePosition at -1.

When parsing "13:01 PM" with a "hh:mm a" pattern, the error
index is corretly set to 8.
But if i try to parse "00:01 PM" with the same pattern, no
error is detected and the error index stays at -1.

Same behaviour with the "kk:mm a" pattern which
accept "00:01 PM" but not "25:01 PM".

Is this a bug or a problem in the documentation?


EXPECTED VERSUS ACTUAL BEHAVIOR :
It looks like the "h" pattern is really 0-12 and the "k"
pattern 0-24.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.text.*;
import java.util.*;

public class BugTest
{

  public static void main (String[] args)
  {
    SimpleDateFormat formater_KK = new SimpleDateFormat("KK:mm a"); // (0-11)
    SimpleDateFormat formater_hh = new SimpleDateFormat("hh:mm a"); // (1-12)
    SimpleDateFormat formater_kk = new SimpleDateFormat("kk:mm a"); // (1-24)

    formater_KK.setLenient(false);
    formater_kk.setLenient(false);
    formater_hh.setLenient(false);

    ParsePosition pos_1 = new ParsePosition(0);
    Date date_1 = formater_KK.parse("12:01 PM",pos_1);
    ParsePosition pos_2 = new ParsePosition(0);
    Date date_2 = formater_hh.parse("13:01 PM",pos_2);
    ParsePosition pos_3 = new ParsePosition(0);
    Date date_3 = formater_hh.parse("00:01 AM",pos_3);
    ParsePosition pos_4 = new ParsePosition(0);
    Date date_4 = formater_kk.parse("00:01 PM",pos_4);

    System.out.println("date_1: " + date_1 + " - index: " + pos_1.getIndex()
                     + " - error index: " + pos_1.getErrorIndex());
    // null
    // OK

    System.out.println("date_2: " + date_2 + " - index: " + pos_2.getIndex()
                     + " - error index: " + pos_2.getErrorIndex());
    // null
    // OK

    System.out.println("date_3: " + date_3 + " - index: " + pos_3.getIndex()
                     + " - error index: " + pos_3.getErrorIndex());
    // Thu Jan 01 12:01:00 GMT 1970
    // NOT OK should be null !
   
    System.out.println("date_4: " + date_4 + " - index: " + pos_4.getIndex()
                     + " - error index: " + pos_4.getErrorIndex());
    // Thu Jan 01 00:01:00 GMT 1970
    // NOT OK should be null !

  }
}
---------- END SOURCE ----------
(Review ID: 153627)
======================================================================

Comments
EVALUATION http://hg.openjdk.java.net/jdk7/build/jdk/rev/3122d9afafd5
25-12-2010

EVALUATION SimpleDateFormat needs to check the parsed value in non-lenient before setting the field to the value.
10-06-2009

EVALUATION Contrary to what one might expect from the DateFormat specification, SimpleDateFormat ignores the lenient setting, and is always rather lenient. The setting is passed on to the calendar, but the interpretation of 1-based hours is done by the formatter, not the calendar. norbert.lindenberg@Eng 2001-06-22
22-06-2001