2

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 java.util.Date, it works for java.sql.Date but not for java.util.Date.

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
    booking.setarrivalDate(a);
    Date d= Date.valueOf(Dept);
    booking.setdeptDate(d);

            booking.setCustomerUsername(username);  
    Long rmnum = Long.valueOf(roomnum);
    booking.setRoomNumber(rmnum);}
1
  • You've shown us where the problem is, but not what the problem is. Can you explain? Commented Mar 27, 2013 at 12:10

2 Answers 2

5

Maybe you're not using a correct format for the Date type and you have to specify the format that you're using, for this purpose use SimpleDateFormat class.

SimpleDateFormat textFormat = new SimpleDateFormat("yyyy-MM-dd");
String paramDateAsString = "2007-12-25";
Date myDate = null;

myDate = textFormat.parse(paramDateAsString);

Hope it helps. Best regards.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

1 Comment

SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yyyy"); Date a=null; try { a = formatter.parse(Arr); } catch (ParseException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } booking.setarrivalDate(a); Date d=null; try { d = formatter.parse(Dept); } catch (ParseException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.