• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

time conversion

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
i want to convert time given in am/pm to 24 hr representation and vice versa
like 19:00 should be parsed in to 7:00 PM
similarly 08:00 PM should be displayed as 20:00 .
how can i do it

from db i get it in datetime format using mysql ie ; 2006-05-18 18:30:22
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use SimpleDateFormat to do that. If you get the datetime from the SQL, I assume you can get Timestamp object.



or



If you just want to convert the time in String from one format to another, you might need to build a Calendar object first, then use SimpleDateFormat to convert it.
 
mudassir shahab
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not working buddy ..see my code ..where is the mistake

String date = "19:00"; // string of date to convert
DateFormat formatIn = new SimpleDateFormat("H:mm");
DateFormat formatOut = new SimpleDateFormat("H:mm a");
Timestamp t = new Timestamp(formatIn.parse(date).getTime());
System.out.println(formatOut.format(new Date(t.getTime())));

output ----> 19:00 PM ......... but the required output is 07:00 PM
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually my reply above is only an example how to use SimpleDateFormat. It's not the solution to your problem. Anyway, here's the solution to your problem.



You don't need to create a Timestamp object. I mentioned the Timestamp object in my previous reply because you mentioned something about retrieving the datetime from mysql. Normally, you will get Timestamp object when retrieving the data from Timestamp.

The formatOut takes "h:mm a" not "H:mm a" if you want to format it in Hour in am/pm (1-12). Check the SimpleDateFormat API for more information.

Do let me know if you have any more questions
[ July 15, 2007: Message edited by: Freddy Wong ]
 
mudassir shahab
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its done ..thanks Wong
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic