Results 1 to 11 of 11
Thread: convert string to a date
- 03-11-2011, 03:28 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 19
- Rep Power
- 0
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:
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()));
'void' type not allowed here
do you have any idea how I can solve that issue please?
- 03-11-2011, 03:43 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,374
- Blog Entries
- 7
- Rep Power
- 17
-
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);
- 03-11-2011, 03:56 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 19
- Rep Power
- 0
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:
then a code from StudentDatabase:Java Code:.... private String enrollmentDate; public void setEnrollmentDate (String newEnrollmentDate) { enrollmentDate = newEnrollmentDate; } public String getEnrollmentDate() { return enrollmentDate; }
I am trying to force user to input date in correct format which should be as requested DD/MM/YYJava 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++; }
now I have:
setEnrollmentDate(java.lang.String) in Student cannot be applied to (java.util.Date) error
-
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.
- 03-14-2011, 11:06 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 19
- Rep Power
- 0
can you tell me what I did wrong here?
If statement does not work as intended, I wanted to have if 'day' is less than 1 and >32 input again.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()); }
thanks in advance
E
-
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));
- 03-14-2011, 11:47 AM #8
Member
- Join Date
- Sep 2010
- Posts
- 19
- Rep Power
- 0
:eek:
thanks! anyway, condition does not work now, it doesnt checkhow i can include scan.nextInt() in if ((tmp.getEnrolmentDay() < 1)&&(tmp.getEnrolmentDay() > 32))? :OJava Code:tmp.setEnrolmentDay(scan.nextInt());
-
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.
- 03-14-2011, 12:01 PM #10
Member
- Join Date
- Sep 2010
- Posts
- 19
- Rep Power
- 0
^^
I guess, I should check this out for specific row in an array so maybe I should go for:
Or something similar?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()); }
but with that I got:
Exception in thread "main" java.lang.NullPointerExceptionLast edited by exose; 03-14-2011 at 12:03 PM.
- 03-14-2011, 12:12 PM #11
Member
- Join Date
- Sep 2010
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Convert String to Date?
By bochra in forum New To JavaReplies: 4Last Post: 11-15-2010, 10:41 AM -
converting string (GMT date) to date in US time.
By JRuyechan in forum New To JavaReplies: 1Last Post: 10-15-2010, 07:07 AM -
convert String to date and pass to PreparedStatement
By computerbum in forum New To JavaReplies: 1Last Post: 09-21-2010, 03:31 AM -
convert String date to Date
By computerbum in forum New To JavaReplies: 7Last Post: 09-18-2010, 03:26 PM -
how we can convert string to date?
By cowboy2010 in forum New To JavaReplies: 4Last Post: 08-25-2010, 08:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks