-4

i am trying to convert "7:05AM" into timestamp but it's not working.

 try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
            Date parsedDate = dateFormat.parse("7:05AM");
            Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
        }
        catch(Exception e)
        {

        }
kishan hadiyal
  • 91
  • 1
  • 2
  • 7

1 Answers1

0

Change SimpleDateFormat ("yyyy-MM-dd hh:mm a") to ("hh:mm a") its working fine

 try {
        SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm a");
        Date parsedDate = dateFormat.parse("7:05 AM");
        Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
        System.out.println(timestamp.toString());
    }
    catch(Exception e)
    {
        System.out.println(e.toString());
    }
sasikumar
  • 12,540
  • 3
  • 28
  • 48
  • it's not working. giving the exception Unparseable date "7 :05 AM" – kishan hadiyal Feb 08 '18 at 07:31
  • 1
    This will work, it will give you an output like this 1970-01-01 07:05:00.0 You class file should contain the following import statements `import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.Date;` – Sony Feb 08 '18 at 08:10