JDK-4689930 : Calendar: AM_PM not recognized when setting HOUR
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:i18n
  • Affected Version: 1.3.0
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2002-05-22
  • Updated: 2002-05-23
  • Resolved: 2002-05-23
Related Reports
Relates :  
Relates :  
Description

Name: nt126004			Date: 05/22/2002


FULL PRODUCT VERSION :
java version "1.3.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_01)
Java HotSpot(TM) Client VM (build 1.3.1_01, mixed mode)

FULL OPERATING SYSTEM VERSION :
Windows NT Version 4.0

A DESCRIPTION OF THE PROBLEM :
When I try to set 12:00 PM to a Calendar Object which is
initialized with a Timestamp(Using Calendar's setTime
method), the date is increased by one.

For Ex, When I try to set 12:00 Pm to 05/20/2002, it
becomes 05/21/2002. But when I call
           cal.get(Calendar.AM_PM), it seems to
work fine.Pls see the code snippet.



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1.Instantiate a Calendar Object by Calendar cal =
cal.getInstance();
2.set Current Time stamp to the cal by cal.setTime()
3.Set the Hours to 12, Mins to 0, and AM PM to PM.


EXPECTED VERSUS ACTUAL BEHAVIOR :
Expected :
If the date is 05/20/2002, after setting the values it
should be 05/20/2002 12 PM.

Actual :
The date is incremented by one. The date becomes 05/21/2002


This bug can be reproduced always.

---------- BEGIN SOURCE ----------

Pls have a look at the following Code:

import java.util.*;
import java.sql.*;

public class TimestampTest
{
    public static void main(String[] args)
    {
        Timestamp tsCurrentTimeStamp = new Timestamp(System.currentTimeMillis
());
        Timestamp newTimestamp = getDateWithTime
(tsCurrentTimeStamp,"12","0","PM");
        System.out.println("newTimestamp  "+newTimestamp );
    }

    public static Timestamp getDateWithTime(Timestamp tsEventDate,String
strHours,String strMins,String strAMPM)
    {
        try
        {

            Calendar cal=Calendar.getInstance();
            cal.setTime(tsEventDate);
            cal.set(Calendar.AM_PM,((strAMPM==null || strAMPM.trim().equals("")
                                                    || strAMPM.trim().equals
("AM"))?Calendar.AM:Calendar.PM));
//When the following line is un commented, it seems
//to work fine.
            //cal.get(Calendar.AM_PM);
			// however, calling cal.getTime() does not fix the bug.
            cal.set(Calendar.HOUR,(strHours==null || strHours.trim().equals
(""))?0:Integer.parseInt(strHours));
            cal.set(Calendar.MINUTE,(strMins==null || strMins.trim().equals
(""))?0:Integer.parseInt(strMins));
            return new Timestamp(cal.getTime().getTime());
        } catch ( Exception ex )
        {
            System.out.println("Excpetion in getDateWIthTime() - " +ex );
            return null;
        }
    }
}


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

CUSTOMER WORKAROUND :
Calling the calendar's get method with AM_PM as the
argument seems to be a work around.
(Review ID: 146808) 
======================================================================

Comments
EVALUATION The numbering for HOUR is 0, 1, 2, 3, ..., 11, not 12, 1, 2, ..., 11. So, 12 p.m. means midnight of the next day. ###@###.### 2002-05-22
22-05-2002