Results 1 to 11 of 11
  1. #1
    exose is offline Member
    Join Date
    Sep 2010
    Posts
    19
    Rep Power
    0

    Default convert string to a date

    Hi,

    I am trying to convert string which looks like:

    DD/MM/YY for example 10/11/2010

    I have created some code but I am getting an error:

    Java Code:
    System.out.print("Enrollment Date:");
    //tmp.setEnrollmentDate(scan.next());
    SimpleDateFormat dateFormat = new SimpleDateFormat("DD/MM/yy");
    Date convertedDate = dateFormat.parse(tmp.setEnrollmentDate(scan.next()));
    error:
    'void' type not allowed here

    do you have any idea how I can solve that issue please?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by exose View Post
    Hi,

    I am trying to convert string which looks like:

    DD/MM/YY for example 10/11/2010

    I have created some code but I am getting an error:

    Java Code:
    System.out.print("Enrollment Date:");
    //tmp.setEnrollmentDate(scan.next());
    SimpleDateFormat dateFormat = new SimpleDateFormat("DD/MM/yy");
    Date convertedDate = dateFormat.parse(tmp.setEnrollmentDate(scan.next()));
    error:
    'void' type not allowed here

    do you have any idea how I can solve that issue please?
    What is the return type of your setEnrollmentDate( ... ) method?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You appear to have your methods in the wrong location. I assume that setEnrollmentDate takes a Date parameter, and you're passing it a String. If my assumptions are incorrect, then please give us the necessary details.

    e.g.,

    Java Code:
    String response = scan.nextLine();
    Date convertDate = dateFormat.parse(response);  // do this in a try/catch block
    tmp.setEnrollmentDate(convertDate);

  4. #4
    exose is offline Member
    Join Date
    Sep 2010
    Posts
    19
    Rep Power
    0

    Default

    Well, let me give you more details:

    the requirements for my assignment is:

    Enrolment Date - string in format DD/MM/YY

    So what I did is:

    Student class what has:

    Java Code:
    ....
    private String enrollmentDate; 
    
    public void setEnrollmentDate (String newEnrollmentDate)
    	{
    		enrollmentDate = newEnrollmentDate;
    	}
    
    public String getEnrollmentDate()
        {
          return enrollmentDate;
        }
    then a code from StudentDatabase:

    Java Code:
    	public void addStudent()
    	{
    			Student tmp = new Student();
    
    			System.out.print("Student Name:");
    			tmp.setFirstName(scan.next());
    
    			System.out.print("Student Surname:");
    			tmp.setLastName(scan.next());
    
    			System.out.print("Enrollment Date:");
    			String response = scan.nextLine();
    			//tmp.setEnrollmentDate(scan.next());
    			SimpleDateFormat dateFormat = new SimpleDateFormat("DD/MM/yy");
    			Date convertDate = dateFormat.parse(response);
        		tmp.setEnrollmentDate(convertDate);
    
    			System.out.print("Year on the course:");
    			tmp.setCourseYear(scan.nextInt());
    
    			students[studentCounter] = tmp;
    			studentCounter++;
    	}
    I am trying to force user to input date in correct format which should be as requested DD/MM/YY

    now I have:

    setEnrollmentDate(java.lang.String) in Student cannot be applied to (java.util.Date) error

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So setEnrollmentDate accepts a String, not a Date object -- so then do that -- pass it the String, not a Date object.

    You can try to validate the input with a SimpleDateFormat object or, since it is a simple validation, use a simple regular expression. But don't use the Date retrieved from the SimpleDateFormat's parse in any of your method calls. You'll need to dispose of it.
    Last edited by Fubarable; 03-11-2011 at 04:00 PM.

  6. #6
    exose is offline Member
    Join Date
    Sep 2010
    Posts
    19
    Rep Power
    0

    Default

    can you tell me what I did wrong here?

    Java Code:
    			System.out.print("Enrolment Day:");
    			tmp.setEnrolmentDay(scan.nextInt());
    			if ((tmp.getEnrolmentDay() < 1)&&(tmp.getEnrolmentDay() > 32));
    			{
    				System.out.println("You have typed wrong day, please do it again!");
    				System.out.print("Enrolment Day:");
    				tmp.setEnrolmentDay(scan.nextInt());
    			}
    If statement does not work as intended, I wanted to have if 'day' is less than 1 and >32 input again.

    thanks in advance
    E

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    dude you have a semi-colon after the condition, remove the semi-colon and it will run the if block

    Java Code:
    if ((tmp.getEnrolmentDay() < 1)&&(tmp.getEnrolmentDay() > 32));

  8. #8
    exose is offline Member
    Join Date
    Sep 2010
    Posts
    19
    Rep Power
    0

    Default

    :eek:

    thanks! anyway, condition does not work now, it doesnt check
    Java Code:
    tmp.setEnrolmentDay(scan.nextInt());
    how i can include scan.nextInt() in if ((tmp.getEnrolmentDay() < 1)&&(tmp.getEnrolmentDay() > 32))? :O

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Lol, another silly error by both of us:

    Java Code:
    if ((tmp.getEnrolmentDay() < 1)&&(tmp.getEnrolmentDay() > 32))

    Should've said

    Java Code:
    if ((tmp.getEnrolmentDay() < 1) || (tmp.getEnrolmentDay() > 32))

    i.e. OR, not "AND" because there is no way the day can be <1 AND >32. It can be <1 OR >32 though.
    Last edited by ozzyman; 03-14-2011 at 11:56 AM.

  10. #10
    exose is offline Member
    Join Date
    Sep 2010
    Posts
    19
    Rep Power
    0

    Default

    ^^

    I guess, I should check this out for specific row in an array so maybe I should go for:

    Java Code:
    if ((students[studentCounter].getEnrolmentDay() < 1)||(students[studentCounter].getEnrolmentDay() > 32))
    			{
    				System.out.println("You have typed wrong day, please do it again!");
    				System.out.print("Enrolment Day:");
    				tmp.setEnrolmentDay(scan.nextInt());
    			}
    Or something similar?

    but with that I got:

    Exception in thread "main" java.lang.NullPointerException
    Last edited by exose; 03-14-2011 at 12:03 PM.

  11. #11
    exose is offline Member
    Join Date
    Sep 2010
    Posts
    19
    Rep Power
    0

    Default

    got it to work i just needed to add:

    Java Code:
    students[studentCounter] = tmp;

Similar Threads

  1. Convert String to Date?
    By bochra in forum New To Java
    Replies: 4
    Last Post: 11-15-2010, 10:41 AM
  2. converting string (GMT date) to date in US time.
    By JRuyechan in forum New To Java
    Replies: 1
    Last Post: 10-15-2010, 07:07 AM
  3. convert String to date and pass to PreparedStatement
    By computerbum in forum New To Java
    Replies: 1
    Last Post: 09-21-2010, 03:31 AM
  4. convert String date to Date
    By computerbum in forum New To Java
    Replies: 7
    Last Post: 09-18-2010, 03:26 PM
  5. how we can convert string to date?
    By cowboy2010 in forum New To Java
    Replies: 4
    Last Post: 08-25-2010, 08:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •