You can use SimpleDateFormat to do that. If you get the datetime from the SQL, I assume you can get Timestamp object.
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.
SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
Joined: Oct 13, 2003
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
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 ]