JDK-5088703 : (fmt) %tl and %tI format specifiers print "0"/"00" instead of 12 for 12 PM
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-08-18
  • Updated: 2004-11-06
  • Resolved: 2004-11-06
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.
Other JDK 6
5.0u2Fixed 6 b12Fixed
Description
Name: rmT116609			Date: 08/18/2004


FULL PRODUCT VERSION :
java version "1.5.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b51)
Java HotSpot(TM) Client VM (build 1.5.0-beta2-b51, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
SunOS parakeet 5.8 Generic_108528-27 sun4u sparc SUNW,Ultra-60


A DESCRIPTION OF THE PROBLEM :
According to the Javadoc documentation to print the hour part of a date in 12 hour format, you would use %tI.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#dt

'I'  Hour for the 12-hour clock, formatted as two digits with a leading zero as necessary, i.e. 01 - 12.

I found that if you print a time that has an hour of 12 PM, it will print 00 instead of 12.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Wed Aug 18 12:00:00 CDT 2004
12:00 PM

ACTUAL -
Wed Aug 18 12:00:00 CDT 2004
00:00 PM


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Test {
	public static void main(String[] args) {
		try {
			Calendar calendar = new GregorianCalendar(2004, 7, 18, 12, 0, 0);
			Date date = calendar.getTime();
			System.out.println(date);
			System.out.println(String.format("%tI:%tM %tP", date, date, date));
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
You can test for the 12 PM case and just create the string manually with a 12 instead of 00.
(Incident Review ID: 297472) 
======================================================================
###@###.### 10/8/04 22:37 GMT

Comments
SUGGESTED FIX *************** *** 3771,3777 **** case DateTime.HOUR: { // 'l' (1 - 12) -- like I int i = t.get(Calendar.HOUR_OF_DAY); if (c == DateTime.HOUR_0 || c == DateTime.HOUR) ! i = (i == 0 ? 12 : i % 12); Flags flags = (c == DateTime.HOUR_OF_DAY_0 || c == DateTime.HOUR_0 ? Flags.ZERO_PAD --- 3771,3777 ---- case DateTime.HOUR: { // 'l' (1 - 12) -- like I int i = t.get(Calendar.HOUR_OF_DAY); if (c == DateTime.HOUR_0 || c == DateTime.HOUR) ! i = (i == 0 || i == 12 ? 12 : i % 12); Flags flags = (c == DateTime.HOUR_OF_DAY_0 || c == DateTime.HOUR_0 ? Flags.ZERO_PAD ###@###.### 10/8/04 22:37 GMT
08-10-2004

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mustang
19-08-2004

EVALUATION oops. Trivial fix should be applied as soon as possible. -- iag@sfbay 2004-08-18
18-08-2004