Results 1 to 8 of 8
  1. #1
    ss2010 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default Java Calendar AM PM problem

    On my screen i have input fields like

    19 Dec 2001 12:00 PM
    but its getting saved as
    20 Dec 2001 12:00 AM

    code that changes the gui input boxes to date

    Java Code:
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(displayDate.getDay()));
    cal.set(Calendar.MONTH, Integer.parseInt(displayDate.getMonth())-1);
    cal.set(Calendar.YEAR, Integer.parseInt(displayDate.getYear()));
    cal.set(Calendar.HOUR, Integer.parseInt(timeHour));
    cal.set(Calendar.MINUTE, Integer.parseInt(timeMinute));
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND,0);
    int amPm = Calendar.AM;
    if(PROMPT_AM.equalsIgnoreCase(timeAmPmMarker)){
         amPm = Calendar.AM;
    } else if (PROMPT_PM.equalsIgnoreCase(timeAmPmMarker)){
         amPm = Calendar.PM;
    }
    cal.set(Calendar.AM_PM, amPm);
    Why is it getting changed ? Any ideas what i am doing wrong ?

    This doesnt happen for any other case ..for eg if i have 11:45 PM it stays that way but 12 AM changes to 12 PM and vice versa with a day change as well.

    The calender object is stored in the db as part of xml ....code which formats is as below
    Java Code:
    GregorianCalendar calendar = (GregorianCalendar) source;
    SimpleDateFormat format = new SimpleDateFormat("dd MMMMMM yyyy hh:mm aa");
    String formattedDate = format.format(calendar.getTime());
    Any help appreciated. Ta
    Last edited by ss2010; 01-06-2011 at 06:14 AM. Reason: more info

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ss2010 View Post
    On my screen i have input fields like

    19 Dec 2001 12:00 PM
    but its getting saved as
    20 Dec 2001 12:00 AM
    A 12 hour clock runs as follows: midnight is shown as 12:00AM (0:00 on a 24 hour clock); twelve hours later (noon) is shown as 12:00pm (12:00 on a 24 hour clock). Also see this link.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ss2010 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Joe,
    Thanks.
    But 19th 12:00pm is surely noon and 20th 12:00am is 12 hrs after that ? What am i missing ?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ss2010 View Post
    Joe,
    Thanks.
    But 19th 12:00pm is surely noon and 20th 12:00am is 12 hrs after that ? What am i missing ?
    I think (I haven't checked it yet) that the answer is somewhere in this part of the API documentation of the Calendar class:

    Quote Originally Posted by API Calendar
    If there is any conflict in calendar field values, Calendar gives priorities to calendar fields that have been set more recently. The following are the default combinations of the calendar fields. The most recent combination, as determined by the most recently set single field, will be used.

    [ ... ]

    For the time of day fields:
    HOUR_OF_DAY
    AM_PM + HOUR

    If there are any calendar fields whose values haven't been set in the selected field combination, Calendar uses their default values. The default value of each field may vary by concrete calendar systems. For example, in GregorianCalendar, the default of a field is the same as that of the start of the Epoch: i.e., YEAR = 1970, MONTH = JANUARY, DAY_OF_MONTH = 1, etc.

    Note: There are certain possible ambiguities in interpretation of certain singular times, which are resolved in the following ways:

    1.23:59 is the last minute of the day and 00:00 is the first minute of the next day. Thus, 23:59 on Dec 31, 1999 < 00:00 on Jan 1, 2000 < 00:01 on Jan 1, 2000.

    2.Although historically not precise, midnight also belongs to "am", and noon belongs to "pm", so on the same day, 12:00 am (midnight) < 12:01 am, and 12:00 pm (noon) < 12:01 pm

    The date or time format strings are not part of the definition of a calendar, as those must be modifiable or overridable by the user at runtime. Use DateFormat to format dates.
    Maybe if you first set the AM/PM part before you set the hours it'll work as you expect it to work.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ss2010 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Setting am/pm before did not work. Also I read somewhere Calendar.set(Calendar.AM_PM,x) does not work & we have to use add(). Anyways I got around the problem by setting all values in the calendar together rather than separate. And then writing some code to handle am/pm eg for PM when hr not 12 add PM (or 12 hrs) to it. Seems to work.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ss2010 View Post
    Setting am/pm before did not work. Also I read somewhere Calendar.set(Calendar.AM_PM,x) does not work & we have to use add(). Anyways I got around the problem by setting all values in the calendar together rather than separate. And then writing some code to handle am/pm eg for PM when hr not 12 add PM (or 12 hrs) to it. Seems to work.
    Good; the Calendar API is terrible (IMHO of course) and I didn't know that those 12 o'clocks twice a day were such a mess. Does (re)setting the 'lenient' flag change its behaviour?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    ss2010 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Not sure if lenient flag helps. Didn't experiment with that too much !

  8. #8
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    Setting am/pm before did not work.
    I did some experimenting for the fun of it and came up with this to further confuse
    anyone interested enough to read it:
    The Calendar class stores the hour internally in the 24 hour format.
    When the hour is set() it is assuming it to be in 12 hour format. (Here anyway)
    So when the hour is set, it adds 12 to it if AM_PM == PM.
    But if AM_PM == AM it doesn't add 12 hours but switches the flag to PM if hour >=12.
    The AM_PM flag is set to PM by default. :confused:
    What seems to work is to set AM_PM to AM before setting the hour and use 0 to 23
    to set it. Then leave it alone.

Similar Threads

  1. Problem with compile Calendar
    By santunez in forum New To Java
    Replies: 1
    Last Post: 05-06-2010, 12:36 AM
  2. Calendar in java - problem
    By omygoodness in forum New To Java
    Replies: 7
    Last Post: 01-04-2010, 08:33 PM
  3. Calendar - problem in Cprompt
    By pinkdreammsss in forum New To Java
    Replies: 8
    Last Post: 04-14-2009, 01:09 PM
  4. Calendar language problem
    By kopros in forum New To Java
    Replies: 1
    Last Post: 10-27-2008, 10:00 PM
  5. Problem with calendar
    By Felissa in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 08:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •