Update lines 30~34 (remove the part final JTextField) because they are local & you added these local variables instead of the class variables.
// should look similar to below
t1 = new JTextField(10);
t2 = new JTextField(10);
t3 = new JTextField(10);
t4 = new JTextField(10);
t5 = new JTextField(10);
Lines 36~41, remove the leading part JLabel from each line.
// should look similar to below
l6 = new JLabel("fine");
l1 = new JLabel("Book Name");
l2 = new JLabel("Author Namae");
l3 = new JLabel("Issue Date");
l4 = new JLabel("Last Date");
l5 = new JLabel("Submitted Date");
Lines 43-44, remove the leading part JButton from each line.
// should look similar to below
b1=new JButton("Issue");
b2=new JButton("Clear");
Line 48, remove the leading part JPanel from the line (same with lines 59 and 71)...
// should look similar to below
p=new JPanel();
It is obvious that you have no idea of variable scope. I blame your instructor that he/she does not teach students about variable scope before letting them code!
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17
Please explain what "not working" whenever you ask a question. Often times, I won't know what the meaning of "not working" and it is difficult to assume.
Now, my assumption from your "not working" is from many causes.
1) Line 28, the pattern string of a SimpleDateFormat does NOT have Y in it. Also, the D will give you the day of the year instead of the day of month. The pattern string you are looking for should be "dd/MM/yyyy".
2) Your catch() definition should be more explicit that just Exception: *. Also, the +se will produce garbage instead of any information. What you should output is to your *l6 variable saying that the input format is invalid. Don't forget to have another catch to cover all other exceptions.
//i.e.
catch (ParseException se) { l6.setText("Invalid input date format"); }
catch (Exception e) {
l6.setText("Unknown exception");
e.printStackTrace();
}
3) You have not handled any input if the fields are empty? If not, the program will always throw the ParseException.
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17
Please look at my previous post and adjust your code accordingly. Your SimpleDateFormat should throw an exception while running because it doesn't recognize the format "DD/MM/YYYY" because the "Y" does not exist in its string pattern list.
Also, I'm guessing that your t5 field is the last date and the t4 field is the submitted date? You should and must name your variables to a meaningful name, so that it can describe itself. Putting only one letter and a number to make it unique does NOT help others especially yourself when you do debugging.
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17
The parsing will throw exception if the values in t4 and t5 do not follow the format. For example, you want 14 November, 2011 to be the value of t4 and 1 December, 2011 to be in t5, you must enter 14112011 in t4 field and 01122011 in t5. If you do not follow the format, you will get either a wrong result or the invalid input.
Below is an example of parsing input string using your string pattern. Look closely for the output.
import java.util.*;
import java.text.*;
class TestSimpleDatePattern {
public static void main(String[] args) {
try {
SimpleDateFormat spd = new SimpleDateFormat("ddMMyyyy");
Date d = spd.parse("18022011"); // correct format
System.out.println(DateFormat.getDateTimeInstance().format(d));
Date d1 = spd.parse("8022011"); // incomplete --> incorrect parsing
System.out.println(DateFormat.getDateTimeInstance().format(d1));
Date d2 = spd.parse("822011"); // incomplete --> incorrect parsing
System.out.println(DateFormat.getDateTimeInstance().format(d2));
Date d3 = spd.parse("Nov 21, 2012"); // Parsing Error!
System.out.println(DateFormat.getDateTimeInstance().format(d3));
}
catch (ParseException se) {
System.out.println("Parse Error!");
se.printStackTrace(); // this will display the string which causes error
}
catch (Exception e) {
System.out.println("Unknown Error!");
e.printStackTrace();
}
}
} // end class
/*
The output:
>java TestSimpleDatePattern
Feb 18, 2011 12:00:00 AM
Dec 19, 0012 12:00:00 AM
Oct 21, 0012 12:00:00 AM
Parse Error!
java.text.ParseException: Unparseable date: "Nov 21, 2012"
at java.text.DateFormat.parse(DateFormat.java:354)
at TestSimpleDatePattern.main(TestSimpleDatePattern.java:13)
*/
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17
.getText() and parse the result to a Date()
stultuske
Industrious Poster
4,379 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
In your original posted code, between line 100 and 101, insert this line of code to see whether the string from getText() from your text field object is correct.
System.out.println("T4: "+s2+" | T5: "+s3);
After you run and tried it, look in the command window (the black background) to see what it said. If you are using an IDE, the message should appear in the debugging area (usually at the bottom?).
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17
That's OK. Taywin's code is to check that you are getting the String from the text field correctly. Now we know that's OK we can move on. Taywin has also posted a lot of code that shows how you should approach the parsing. What part of that is causing you a problem? If your code isn't parsing as you expect then you will need to post the code so we can see what's wrong with it.
JamesCherrill
... trying to help
8,521 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30
That parsing looks right, depending on how sdf has been defined. Can you post the definition of sdf, and an example of the text that's not parsing properly?
JamesCherrill
... trying to help
8,521 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30
Yes, I know what a simple date format is. The question is what format are you using? What is the string in quote marks inside the parenthesis after the text SimpleDateFormat in your code?
JamesCherrill
... trying to help
8,521 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30