louise louise - 1 year ago 48
Java Question

java util date value of string

This is a method in an EJB for adding booking, I'm trying to set a to value of String Arr because Arr is a string which gets the form value in the Servlet and I want to do the same for d to value of String Dept. I'm using

, it works for
but not for

public void addBooking(String Arr, String Dept, String username, String roomnum){
BookingTableClass booking = new BookingTableClass();

Date a= Date.valueOf(Arr);//the problem is in these four lines
Date d= Date.valueOf(Dept);

Long rmnum = Long.valueOf(roomnum);

Answer Source

You can convert a java.util.Date into a java.sql.Date object like this:

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

That is, you obtain the java.util.Date equivalent in milliseconds and then use this value to initialize the java.sql.Date object.