aspose file tools*
The moose likes Beginning Java and the fly likes SimpleDateFormat class showing AM when it should be PM & vice versa? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "SimpleDateFormat class showing AM when it should be PM & vice versa?" Watch "SimpleDateFormat class showing AM when it should be PM & vice versa?" New topic
Author

SimpleDateFormat class showing AM when it should be PM & vice versa?

Reed Peters
Ranch Hand

Joined: Jul 23, 2002
Posts: 38
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
I had something like this a while back. I think the answer is to use HH instead of hh in the output pattern.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Your problem is here:

the two lower case h's represent the hours for a 12 hour clock. Use capital H's (24 hour clock)and your problem should be solved.

Jamie
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

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
Thank you Jamie the HOUR_OF_DAY fixed it!
 
 
subject: SimpleDateFormat class showing AM when it should be PM & vice versa?