import javax.swing.* ;
 import java.awt.event.*;
 import java.awt.*;
 import java.io.*;
 import java.util.Calendar;
 import java.util.*;
 import java.text.*;
 import java.util.Date;


  /*
  <applet code="gui.class" width=250 height=100>
  </applet>
  */


 public class gui1  extends  JApplet  implements  ActionListener
 {
     JTextField t1,t2,t3,t4,t5;
     JLabel l1,l2,l3,l4,l5,l6;
     JButton b1,b2;
     Calendar c1,c2,c3;
     JPanel p,p1,p2;
     Date sd,sd1;
     SimpleDateFormat sdf;
     public void init()
     {
        SimpleDateFormat sdf = new SimpleDateFormat("DD/MM/YYYY");

         final  JTextField t1 = new JTextField(10);
         final  JTextField t2 = new JTextField(10);
         final  JTextField t3 = new JTextField(10);
         final  JTextField t4 = new JTextField(10);
         final  JTextField t5 = new JTextField(10);

         JLabel l6 = new JLabel("fine");
         JLabel l1 = new JLabel("Book Name");
         JLabel l2 = new JLabel("Author Namae");
         JLabel l3 = new JLabel("Issue Date");
         JLabel l4 = new JLabel("Last Date");
         JLabel l5 = new JLabel("Submitted Date");

          JButton b1=new JButton("Issue");
          JButton b2=new JButton("Clear");

          setLayout(new BorderLayout());

          JPanel p=new JPanel();
           p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));  
           p.add(l1);

           p.add(l2);

           p.add(l3);


           p.add(l4);
           p.add(l5);
            JPanel p1=new JPanel();
             p1.setLayout(new BoxLayout(p1,BoxLayout.Y_AXIS));
            p1.add(t1);
           p1.add(t3);
            p1.add(t2);
           p1.add(t4);

           p1.add(t5);

       add(p,BorderLayout.WEST);
       add(p1,BorderLayout.EAST);

        JPanel p2=new JPanel();
         p2.add(b1);
         p2.add(b2);
          p2.add(l6);         
         add(p2,BorderLayout.SOUTH);

          b1.addActionListener(this);
          b2.addActionListener(this);
     }

     public void actionPerformed(ActionEvent e)
     {
         String s,s1,s2,s3;
    s=e.getActionCommand();

    if(s.equals("Issue"))
    {
        t1.setText("");
        t2.setText("");
        t3.setText(""); 
        t4.setText("");
        t5.setText("");
    }

      if(s.equals("Clear"))
    {

           s2=t4.getText();
           s3=t5.getText();
           try{
           sd=sdf.parse(s2);
           sd1=sdf.parse(s3);

         long day1=(sd1.getTime() - sd.getTime());
        long day=day1/(24*60*60*1000);                


      if(day <= 0)
      {
          l6.setText("no fine");
      }
      else
      {
          l6.setText("fine of Rs" +day);
      }
           } //try close
           catch (ParseException se)
           {
               System.out.println("Exception :"+se);
           }
           } //else close
    }
     }


This is my new code i have just added the card layout tats all sir **taywin**
nmaillet commented: Give specifics when asking for help. -1

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!

sir the issue button is working but clear button is not sirtaywin

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.

Sir according to my parogram when i press the issue button it should take the input from last date and submitted date and then it should find the differnce between them but in my case its not happening,so what may be the reason for this.

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.

even after changing all the things u have told its not working anyways thanx a lot sir

Is there any otherway i can convert a date into a string and find the differece between the two dates, the problem is while parsing sir now i came to know because whn i press the clear button its going to catch block and dispalying invalid input. Taywin

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)
*/
commented: nice example +2

actually i am following the same format, even though i am getting the invalid input sir. if you don mind can you see my parsing is correct or not.

How to get a date from jtextfield ? anybody please help

.getText() and parse the result to a Date()

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?).

i have tried tat also sir its giving the date whc has been enterd in textfield, the problem is while pasing sir

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.

import javax.swing.* ;
 import java.awt.event.*;
 import java.awt.*;
 import java.io.*;
 import java.util.Calendar;
 import java.util.*;
 import java.text.*;
 import java.util.Date;


  /*
  <applet code="gui.class" width=250 height=100>
  </applet>
  */


 public class gui1  extends  JApplet  implements  ActionListener
 {
     JTextField t1,t2,t3,t4,t5;
     JLabel l1,l2,l3,l4,l5,l6;
     JButton b1,b2;
     Calendar c1,c2,c3;
     JPanel p,p1,p2;

     SimpleDateFormat sdf;
     public void init()
     {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

           t1 = new JTextField(10);
           t2 = new JTextField(10);
            t3 = new JTextField(10);
           t4 = new JTextField(10);
         t5 = new JTextField(10);                 

         l6 = new JLabel("");
        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");

          b1=new JButton("Issue");
           b2=new JButton("Clear");

          setLayout(new BorderLayout());

          p=new JPanel();
           p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));  
           p.add(l1);

           p.add(l2);

           p.add(l3);


           p.add(l4);
           p.add(l5);
            p1=new JPanel();
             p1.setLayout(new BoxLayout(p1,BoxLayout.Y_AXIS));
            p1.add(t1);
           p1.add(t3);
            p1.add(t2);
           p1.add(t4);

           p1.add(t5);

       add(p,BorderLayout.WEST);
       add(p1,BorderLayout.EAST);

         p2=new JPanel();
         p2.add(b1);
         p2.add(b2);
          p2.add(l6);         
         add(p2,BorderLayout.SOUTH);

          b1.addActionListener(this);
          b2.addActionListener(this);
     }

     public void actionPerformed(ActionEvent es)
     {
         String s,s1,s2,s3;
    s=es.getActionCommand();

    if(s.equals("Issue"))
    {
        t1.setText("");
        t2.setText("");
        t3.setText(""); 
        t4.setText("");
        t5.setText("");
    }

      if(s.equals("Clear"))
        {


               s2=t4.getText();
               s3=t5.getText();
               try{


            Date    sd= (Date) sdf.parse(s2);    **this part oly sir **
            Date    sd1= (Date) sdf.parse(s3);    **this part oly sir** 
             long day1=(sd1.getTime() - sd.getTime());
            long day=day1/(24*60*60*1000);                


          if(day <= 0)
          {
              l6.setText("no fine");
          }
          else
          {
              l6.setText("fine of Rs" +day);
          }
           }  // try close
              catch(Exception e)
              {
            l6.setText("Error : Invalid Date "); 

              }      
               } //else close
        }
         }

The pasing whc i hav done is correct or wrong

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?

sdf means simpledateformat whc helps in formating and parsing the date in whc way we want it.

example is lastdate: 10/10/2012
            submitted date: 20/10/2012

            output: invalid date format

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?

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

Parsing "10/10/2012" and "20/10/2012" with a SimpleDateFormat of "dd/MM/yyyy" works perfectly, so your code isn't doing what you think it is...
To find out what's really happening, try putting a lot more print statements into the code, eg print s2 and s3 (with a delimiter immediately before and after so you can see any leading/trailing blanks). Do an e.printStackTrace(); in your catch

Have you added the e.printStackTrace(); yet? I just spotted your mistake, and you will be surprised by what the stack trace reveals.

what is the mistake sir

What does the printStackTrace tell you?

I found your error...

From the original code post, line 25, you declare SimpleDateFormat as the class variable, but then line 28 you declare a local variable with the same name to add in your panel. As a result, your call is not calling from the one you initilised but the class variable.

Update your line 28 to be...

sdf = new SimpleDateFormat("dd/MM/yyyy");

Yes, I'm sure quite a few of us did. If the OP had followed my suggestion he would have seen the resulting N.P.E. and debugged that for himself. And worked out his own solution.

The problem is that the OP doesn't understand the variable scope at all...

Also, James you forgot one thing... At first, I thought that the OP is familiar enough to be using an IDE or command line to work (and display the applet). Instead, it seems that the OP is using HTML file with embeded applet tag to run the applet (from the comment in the file which could be from the instructor). As a result, the OP doesn't know where to look for the error when the System.out.println() does not work on there. Even worse, it is obvious that the OP doesn't even know how to display error correctly onto the applet.

This link is about scopes but may be too much for a beginner. This youtube video may be a bit more clear.

anways thanx a lot sir

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.