• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Reed Peters
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Lance Finney
Ranch Hand
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jamie the HOUR_OF_DAY fixed it!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic