0

My problem is the date entered in database from JSP is today's date instead of date chosen by user.

JSP file:

<td><input type="date" name="availableDate" /></td>

Servlet :

String[] presentationID = request.getParameterValues("selectavailability");
String[] availableDay =  request.getParameterValues("availableDay"); 
String[] availableStart =  request.getParameterValues("availableStart");
String[] availableEnd =  request.getParameterValues("availableEnd");
String[] availableDate = request.getParameterValues("availableDate");  

SimpleDateFormat availDate = new SimpleDateFormat("yyyy-MM-dd");

try
{

 for (int i = 0; i < availableDay.length; i++) 
 {

   AvailabilityBean available = new  AvailabilityBean();         

   available.setLecturerID(request.getParameter("lecturerID"));
   available.setAvailableDay(availableDay[i]);
   available.setAvailableStart(availableStart[i]);
   available.setAvailableEnd(availableEnd[i]);

   Date chosenDate = availDate.parse(availableDate[i]);
   available.setAvailableDate(chosenDate);
   available = AddAvailableDAO.addavailable(available);
   }
   }

DAO:

 String lecturerID = Abean.getLecturerID();
    String availableDay = Abean.getAvailableDay();
    String availableStart = Abean.getAvailableStart();
    String availableEnd = Abean.getAvailableEnd();
   // Date availableDate = Abean.getAvailableDate();
    String presentationID = Abean.getPresentationID();

try{  


    currentCon = JavaConnectionDB.getConnection();

    Date availableDate = Abean.getAvailableDate();           
   // java.util.Date date = new java.util.Date();
    java.sql.Date sqlDate = new java.sql.Date(availableDate.getTime()); //I THINK SOMETHING IS WRONG HERE

    PreparedStatement ps=currentCon.prepareStatement("insert into availability (availableID,lecturerID,availableDay,availableStart,availableEnd,availableDate,presentationid) values (availabilityseq.nextval,?,?,?,?,?,?)");  
            ps.setString(1,Abean.getLecturerID());  
            ps.setString(2,Abean.getAvailableDay());  
            ps.setString(3,Abean.getAvailableStart()); 
            ps.setString(4,Abean.getAvailableEnd()); 
            ps.setDate(5,sqlDate);        
            ps.setString(6,Abean.getPresentationID()); 
            ps.executeUpdate();

 }

Bean:

    private String availableID;
    private String lecturerID;
    private String availableDay;
    private String availableStart;
    private String availableEnd;
    private Date availableDate;
    private String presentationID;

How can I solve the problem?

EDIT: I added some part of my servlet which consist of parsing the value of availableDate and DAO

assylias
  • 321,522
  • 82
  • 660
  • 783
Elly
  • 97
  • 1
  • 1
  • 9

4 Answers4

1

This code is wrong:

java.util.Date date = new java.util.Date();

Here you are creating a new Date object, its value will be always the current date.

You should extract the user selected date value from your AvailabilityBean object in Dao method

. Considering your code, I assume the changed code should be like below:

java.sql.Date sqlDate = new java.sql.Date(Abean.getAvailableDate().getTime());
Amit Bhati
  • 5,569
  • 1
  • 24
  • 45
1

Here you only creating current date object and you are not setting the user selected date value.

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

Try the following code:

//1. assuming Abean returning date in string value.
String availableDate = Abean.getAvailableDate();

// parsing the availableDate to string to date object.
DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); // you can use your own date format here instead of 'MM/dd/yyyy'
Date choosenDate = df.parse(availableDate);

// coverting java date object to java.sql date object.
java.sql.Date sqlDate = new java.sql.Date(choosenDate.getTime());

EDIT: If availableDate is a date object then you can directly use the following:

Date availableDate = Abean.getAvailableDate();
java.sql.Date sqlDate = new java.sql.Date(availableDate.getTime());

Now you will have the user selected date in sqlDate variable.

Srinu Chinka
  • 1,471
  • 13
  • 19
  • Well now it is working BUT I am getting some weird date. For example, I entered 22-Mar-2016, in database it will be 29-Apr-2027 – Elly Apr 14 '16 at 07:05
  • @Elly how you are setting user selected date to your bean object ? you are performing any parsing operations ? if yes please add the date parsing code to your question, so that we can tell where is the problem, is the problem sql date conversion or user date conversion. – Srinu Chinka Apr 14 '16 at 07:08
  • @Elly the code looks fine, i think the problem is with the string to date format conversion, in which format user is going the enter in text box ? – Srinu Chinka Apr 14 '16 at 07:27
  • Hmm maybe, cause the format for input date in JSP is 04/15/2016. How can i change that -.- – Elly Apr 14 '16 at 07:30
  • Yeah, for `type="date"` browser taking date format as `MM/DD/YYYY` so you can change date format pattern like this in your servlet ` SimpleDateFormat availDate = new SimpleDateFormat("MM/dd/yyyy");`. give it a try and let me know it's working or not. – Srinu Chinka Apr 14 '16 at 07:39
  • @Sirnu its not working haha. but i tried `yyyy-mm-dd` because i saw my exception error was in `yyyy-mm-dd` format.... It is kinda working except that even though i choose July, it will become January haha – Elly Apr 14 '16 at 07:43
  • you tried with `yyyy-MM-dd` or `yyyy-mm-dd`? In Java `MM` for month and `mm` for minutes, by what you are saying i think you are using `yyyy-mm-dd`. please check with format. – Srinu Chinka Apr 14 '16 at 07:48
  • SOLVED! You are right! I should use `yyyy-MM-dd`. Thank you very much :D – Elly Apr 14 '16 at 08:01
0

you are creating a new Date, just before creating the 'PreparedStatement' in:

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

With this code you are getting the 'Today date' and later, in 'ps' you add in the parameter # 5.

PreparedStatement ps=currentCon.prepareStatement("insert into availability (availableID,lecturerID,availableDay,availableStart,availableEnd,availableDate,presentationid) values (availabilityseq.nextval,?,?,?,?,?,?)");  
        ps.setString(1,Abean.getLecturerID());  
        ps.setString(2,Abean.getAvailableDay());  
        ps.setString(3,Abean.getAvailableStart()); 
        ps.setString(4,Abean.getAvailableEnd()); 
        **ps.setDate(5,sqlDate**);        
        ps.setString(6,Abean.getPresentationID()); 
        ps.executeUpdate();

That is the reason you are getting in the DB the 'Today´s date'. Use in the Parameter # 5 the call to the appropiate method:

ps.setDate(5,Abean.getAvailableDate());
imalfabon
  • 53
  • 4
  • i got this error: `incompatible types: java.util.Date cannot be converted to java.sql.Date` for that... – Elly Apr 14 '16 at 07:04
  • You can't use a `java.util.Date` with `PreparedStatement.setDate`. – Mark Rotteveel Apr 14 '16 at 07:41
  • Then you can just make a conversion: ps.setDate(5, new java.sql.Date(Abean.getAvailableDate().getTime())); – imalfabon Apr 16 '16 at 02:53
  • Basil Bourque is right, java.time is the new way, but that is not the question, right? Because then the answer could have gone to recommend use not JSP but JSF or other framework, according to http://stackoverflow.com/questions/23707036/java-ee-vs-jsp-vs-jsf JSP is almost 'dead'. – imalfabon Apr 16 '16 at 03:19
0

The other answers are correct and informative. But they use old outmoded classes.

java.time

The java.time framework supplants the old date-time classes bundled with the earliest versions of Java. The old classes are poorly designed, confusing, and troublesome.

Do not use java.util.Date, java.util.Calendar, java.text.SimpleDateFormat and their siblings. Sun and Oracle gave up on those classes, deciding to build-in java.time classes as their replacement.

The java.time classes are built into Java 8 and later. See Oracle Tutorial. Much of their functionality has been back-ported to Java 6 & 7 and to Android. Defined by JSR 310, inspired by Joda-Time, and extended by ThreeTen-Extra. The java.time framework is the official successor to Joda-Time, though you can employ both within a project if you are careful with your import statements as some classes share the same name. Search Stack Overflow for much more info about java.time.

LocalDate

Among the new classes is LocalDate to represent a date-only value without time-of-day and without time zone.

LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;

You can instantiate by parsing a string or specifying numbers.

LocalDate localDate = LocalDate.parse( "2016-01-23" );
LocalDate localDate = LocalDate.of( 2016 , 1 , 23 );

SQL & JDBC

Hopefully JDBC drivers will be updated to directly use the java.time types. Until then we must use java.sql types to move data in/out of databases. Find new methods added to the old classes to facilitate converting to/from java.time types.

java.sql.Date sqlDate = java.sql.Date.valueOf( localDate );

Going the other direction.

LocalDate localDate = sqlDate.toLocalDate();

Minimize your use of java.sql.Date; use only for transfer with database. Use the java.time for your business logic.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • I am sorry since I am new with `datetime`. What i undersand is, use `java.util.Date`, `java.util.Calendar`, `java.text.SimpleDateFormat` only to pass date into database. For logic, i need to use `java.time` cause it is better. So i can use these libraries by mixing their usage? It's not a problem right? – Elly Apr 15 '16 at 03:52
  • @Elly Apparently you missed the part where I said: …Do not use `java.util.Date`, `java.util.Calendar`, `java.text.SimpleDateFormat`… – Basil Bourque Apr 15 '16 at 04:08
  • OHHH hah okay. What about joda time? – Elly Apr 15 '16 at 04:15
  • The java.time classes supplant the Joda-Time library. Designed and led by the same man that invented Joda-Time, uses same basic concepts. We have been told to migrate from Joda-Time to java.time as soon as is convenient though Joda-Time continues to receive updates. Further development will happen only on java.time and its extension, the [ThreeTen-Extra](http://www.threeten.org/threeten-extra/) project. Also, back-ported to Java 6 & 7 in [ThreeTen-Backport](http://www.threeten.org/threetenbp/) and to Android in [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). – Basil Bourque Apr 15 '16 at 05:45
  • I posted [this Question with Answer](http://stackoverflow.com/q/36639154/642706) with a nifty diagram to explain the various old and new classes and how to convert. That may help you with the basics, though I did not cover the java.sql types. – Basil Bourque Apr 15 '16 at 05:51