0

Hi in my PostgreSQL in using time without time zone to save a specific time (19:36:17.376), and in my web application in using jQuery date picket for picking a time from client side for search by time in the format(12:30 PM). How to convert the time 12:30 PMto this format(19:36:17.376).

i am using hibernate for searching. searching java code

String queryString = "from TicketBooking  t where t.isSale='true' ";    queryString = queryString
                    + " and t.time BETWEEN :stTime AND :edTime";  SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
             SimpleDateFormat parseFormat = new SimpleDateFormat("yyy-mm-dd hh:mm a");
               Date date1 = parseFormat.parse(fromTime);
               Date date2 = parseFormat.parse(toTime);
               System.out.println( displayFormat.format(date1)+"------------------"+displayFormat.format(date2));
                        query.setParameter("stTime", displayFormat.format(date1));          query.setParameter("edTime", displayFormat.format(date2));
Aadil Keshwani
  • 1,385
  • 1
  • 18
  • 29
jijesh
  • 35
  • 2
  • 6
  • `SimpleDateFormat("hh:mm a")` to convert the `String` to a `Date`, `SimpleDateFormat("HH:mm:ss.SSS")` to convert the `Date` to a `String` – MadProgrammer Feb 06 '15 at 05:50
  • http://stackoverflow.com/questions/6907968/how-to-convert-24-hr-format-time-in-to-12-hr-format –  Feb 06 '15 at 05:55
  • getting below exception INFO : org.hibernate.engine.jdbc.internal.LogicalConnectionImpl - HHH000106: For cing container resource cleanup on transaction completion – jijesh Feb 06 '15 at 05:58

2 Answers2

1

Convert 12:30 PM to Date...

SimpleDateFormat inFormat = new SimpleDateFormat("hh:mm a");
Date date = inFormat.parse("12:30 PM");

Convert Date to 19:36:17.376 like format...

SimpleDateFormat outFormat = new SimpleDateFormat("HH:mm:ss.SSS");
String out = outFormat.format(date);

Now personally, I'd leave the Date as it is and wrap it within a java.sql.Date and pass that to the database for it to handle...

java.sql.Date sqlDate = new java.sql.Date(date.getTime());

And not worry about trying to format the Date to a String, but that's me

Have a look at java.text.SimpleDateFormat for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

you can try using a SimpleDateFormat object to convert the time formats.

final String time = "23:15";

try {

    final SimpleDateFormat sdf = new SimpleDateFormat("H:mm");

    final Date dateObj = sdf.parse(time);

    System.out.println(dateObj);

    System.out.println(new SimpleDateFormat("K:mm").format(dateObj));


} catch (final ParseException e) {

    e.printStackTrace();


}

here the download link http://developer.android.com/reference/java/text/SimpleDateFormat.html

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Tariq hussain
  • 614
  • 5
  • 11