-2

I am taking the date as input from JSP and then I need to store in the database using hibernate. my date format is also correct but still, it's not working. Here is my code:

String dateStr =  request.getParameter("receive_date");
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = (Date)formatter.parse(dateStr);
System.out.println(date);     

Calendar cal = Calendar.getInstance();
cal.setTime(date);

String formatedDate = cal.get(Calendar.DATE) + "/"+ (cal.get(Calendar.MONTH) + 1) + "/" +   cal.get(Calendar.YEAR); 
out.println("formatedDate : " + formatedDate);
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 2
    Please provide your inputs and expected outputs. – Andrew Fan Sep 02 '18 at 13:11
  • It looks like `request.getParameter()` is getting a blank string from the request. Could this be so? You may want to use your debugger or print the string so you can check. – Ole V.V. Sep 02 '18 at 13:39
  • It doesn’t answer your question, but I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Sep 02 '18 at 13:40

1 Answers1

0

Try changing these two lines

DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = (Date)formatter.parse(dateStr);

to

 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
Date date = LocalDate.parse(dateStr, formatter);
Avijit Barua
  • 2,950
  • 5
  • 14
  • 35