SimpleDateFormat class showing AM when it should be PM & vice versa?
Reed Peters
Ranch Hand
Joined: Jul 23, 2002
Posts: 38
posted
0
Hi, I am using the SimpleDateFormat class to format a time display (hours:minutes AM/PM). Everything works good except the AM/PM settings are appearing backwards. Any idea what I am doing wrong? Here's the code: In a javaBean I set the time from a 4 digit int from a legacy system. For example 1500 is 3pm. public void setPropCheckInTime(int CheckInTime) { int hours = CheckInTime/100; int minutes = CheckInTime - (int)((CheckInTime/100)*100); Calendar cal = Calendar.getInstance(); cal.set(1970, 2, 1); cal.set(Calendar.HOUR, hours); cal.set(Calendar.MINUTE, minutes); cal.set(Calendar.SECOND, 0); this.propCheckInTime = cal.getTime(); } This sets a Date field to Jan 1st 1970 and whatever time is passed in. If 1500 is passed in then hours=15 and minutes=0. In a JSP I am outputing the time this way: I create a time formator: <%SimpleDateFormat formatTime = new SimpleDateFormat("hh:mm aaa");%> and I output the time with: <TD nowrap align="right"><FONT face="Arial Narrow"><B>Check In:</B></FONT></TD> <TD nowrap><%=formatTime.format(propBean.getPropCheckInTime())%></TD> If 1500 was the orginal time comming in I get 3:00 AM when it should be PM. Help! Thanks,
Reed Peters<br />Reed@cm-inc.com
Lance Finney
Ranch Hand
Joined: Apr 26, 2001
Posts: 133
posted
0
I had something like this a while back. I think the answer is to use HH instead of hh in the output pattern.
Actually, I think Lance and I misread your question. The problem is not in formatting the output, but on setting the initial calendar. Same idea though, you are setting the hour of am/pm instead of hour of day. try this when setting your Calendar:
instead of setting Calendar.HOUR. Jamie
Reed Peters
Ranch Hand
Joined: Jul 23, 2002
Posts: 38
posted
0
Thank you Jamie the HOUR_OF_DAY fixed it!
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: SimpleDateFormat class showing AM when it should be PM & vice versa?