Results 1 to 8 of 8
Thread: Java Calendar AM PM problem
- 01-06-2011, 06:11 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
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
Why is it getting changed ? Any ideas what i am doing wrong ?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);
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
Any help appreciated. TaJava Code:GregorianCalendar calendar = (GregorianCalendar) source; SimpleDateFormat format = new SimpleDateFormat("dd MMMMMM yyyy hh:mm aa"); String formattedDate = format.format(calendar.getTime());Last edited by ss2010; 01-06-2011 at 06:14 AM. Reason: more info
- 01-06-2011, 07:17 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-06-2011, 09:43 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
Joe,
Thanks.
But 19th 12:00pm is surely noon and 20th 12:00am is 12 hrs after that ? What am i missing ?
- 01-06-2011, 10:21 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
I think (I haven't checked it yet) that the answer is somewhere in this part of the API documentation of the Calendar class:
Maybe if you first set the AM/PM part before you set the hours it'll work as you expect it to work.
Originally Posted by API Calendar
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-07-2011, 05:30 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
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.
- 01-07-2011, 07:26 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-07-2011, 09:21 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
Not sure if lenient flag helps. Didn't experiment with that too much !
- 01-08-2011, 12:07 AM #8
Member
- Join Date
- Oct 2010
- Posts
- 63
- Rep Power
- 0
I did some experimenting for the fun of it and came up with this to further confuseSetting am/pm before did not work.
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
-
Problem with compile Calendar
By santunez in forum New To JavaReplies: 1Last Post: 05-06-2010, 12:36 AM -
Calendar in java - problem
By omygoodness in forum New To JavaReplies: 7Last Post: 01-04-2010, 08:33 PM -
Calendar - problem in Cprompt
By pinkdreammsss in forum New To JavaReplies: 8Last Post: 04-14-2009, 01:09 PM -
Calendar language problem
By kopros in forum New To JavaReplies: 1Last Post: 10-27-2008, 10:00 PM -
Problem with calendar
By Felissa in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 08:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks