• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jamie the HOUR_OF_DAY fixed it!
 
Are we home yet? Wait, did we forget the tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic