-1
  for (final Object[] obj : leaveObjects) {
             for (int i = 0; i < obj.length; i++){
     try {

            jdbcTemplate.update(new PreparedStatementCreator() {
                public PreparedStatement createPreparedStatement(
                        Connection connection) throws SQLException {
                    PreparedStatement ps = connection.prepareStatement(sql,
                            new String[] { "pk_CompOff_compOffId" });
                    ps.setInt(1,  (Integer)obj[0]);
                    ps.setInt(2,  (Integer)obj[1]);
                    ps.setInt(3,  (Integer)obj[2]);
                    ps.setInt(4, (Integer) obj[3]  );
                    try {
                        ps.setDate(5, new java.sql.Date(formatter.parse(obj[4].toString()).getTime()));
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

I am trying to save this date from java to the database using spring jdbc template using the above code but getting this exception:

java.text.ParseException: Unparseable date: "Sun Feb 15 00:00:00 PST 2015" . i am using this formatter

 SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");

How I can resolve that?

saurabh
  • 237
  • 6
  • 20

2 Answers2

0

change :

SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");

to:

SimpleDateFormat formatter = new SimpleDateFormat("EEEE MMM dd HH:mm:ss z yyyy");

Note:

  1. a is replaced by z
  2. commas are removed
  3. some fields have chnaged places

a used for AM/PM where as z used for Time zone (e.g. PST)

For more reference, check this documentation:

http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

nafas
  • 5,283
  • 3
  • 29
  • 57
0

You have to use the Locale too, otherwise it will not run in every locale.

Try this

SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
chokdee
  • 446
  • 4
  • 13