Page 1 of 2 12 LastLast
Results 1 to 20 of 23
Like Tree2Likes

Thread: Java Date input from User,

  1. #1
    Shahnawaz is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default Java Date input from User,

    Hi,
    i'm new to java and having quite some trouble doing my assignments..
    what i need to do is create a program which takes any Date as input from user and then converts it to long date.. i've tried something out but its not working.. Please Help; Thanks in advance :)
    Java Code:
    import java.util.*;
    import java.text.*;
    public class wy2 {
       public static void main(String[] args) {
         
            Date now = new Date();
          	Scanner sc = new Scanner(System.in);  
            System.out.print("Enter Date: ");  
            String ind = sc.nextLine();  
            	
    		DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
    
    try {
             Date d = new Date(); 
    	   	 d=df.parse(ind);
    	 
          }
          catch(ParseException e) {
             System.out.println("Unable to parse " + ind);
          }
    
          DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG);
     
         
          String s3 = df3.format(d);
       
         
          System.out.println("The entered date is: " + s3);
        
       }
    }
    Last edited by Norm; 09-07-2011 at 01:18 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: Java Date input from User,

    i've tried something out but its not working
    Please post the full text of the error message or show what is not working.

    Add a e.printStackTrace() call to the catch block so you get the FULL text of the error message.

  3. #3
    Shahnawaz is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default Re: Java Date input from User,

    i'm using Jcreator. and this is the error i get when i try to compile the code:

    cannot find symbol
    symbol : variable d
    location: class wy2
    String s3 = df3.format(d);
    ^
    1 error

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: Java Date input from User,

    cannot find symbol
    symbol : variable d
    The compiler can't find a definition for the variable d that is in scope where you are referencing it.
    Move the definition outside of the enclosing {}s so it is within the same pair of {}s where you are trying to use it.
    Define it with a null value and give it a value in the {}s

  5. #5
    Shahnawaz is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default Re: Java Date input from User,

    its not working :(
    can u edit the code please..

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: Java Date input from User,

    its not working
    Please explain and show the error message and the code with the problem

  7. #7
    Shahnawaz is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default Re: Java Date input from User,

    i can't move the code for d out of {}s cuz its in try catch exception..
    i tried this

    import java.util.*;
    import java.text.*;
    public class wy2 {
    public static void main(String[] args) {


    Scanner sc = new Scanner(System.in);
    System.out.print("Enter Date: ");
    String ind = sc.nextLine();
    //Date now = new Date();
    DateFormat df = new SimpleDateFormat("dd/mm/yyyy");

    try {
    Date d = null;
    d=df.parse(ind);

    }
    catch(ParseException e) {
    System.out.println("Unable to parse " + ind);}


    DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG);


    String s3 = df3.format(d);


    System.out.println("The entered date is: " + s3);

    }
    }


    and still the same error...

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Java Date input from User,

    Quote Originally Posted by Shahnawaz View Post
    its not working
    Then you did something wrong.
    can u edit the code please..
    No! It is your homework not ours. We have already been to university and did all our own homework. Now it's your turn.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: Java Date input from User,

    i can't move the code for d out of {}s cuz its in try catch exception..
    What did you try? Move the definition outside of the {}s
    See post #4

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Java Date input from User,

    Quote Originally Posted by Shahnawaz View Post
    i can't move the code for d out of {}s cuz its in try catch exception..
    That is the whole point. Move the "Date d = null" line to be before the try statement.

  11. #11
    Shahnawaz is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default Re: Java Date input from User,

    lolz Junky but actually i'm new to java.. really having a hard time here :(

  12. #12
    Shahnawaz is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default Re: Java Date input from User,

    it worked!
    Thanks Guys :))

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Java Date input from User,

    Quote Originally Posted by Shahnawaz View Post
    lolz Junky but actually i'm new to java..
    In that case don't make statements like "I can't" until you actually try it. You learn a lot by experimenting.
    Shahnawaz likes this.

  14. #14
    Shahnawaz is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default Re: Java Date input from User,

    One more thing.. it always give the output month as January no matter what i enter.. why is it so?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: Java Date input from User,

    Show the input and output from when you execute the program.
    Better yet, hard code a literal String for the user input and post the code.

    String ind = "PUT THE TEST DATE HERE"; //sc.nextLine();

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Java Date input from User,

    I guess it depends upon what getDateInstance method does. Try creating your own SimpleDateFormat so you can specify what format you want the date to appear in.

  17. #17
    Shahnawaz is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default Re: Java Date input from User,

    i changed the code to

    import java.util.*;
    import java.text.*;
    public class wy2 {
    public static void main(String[] args) {


    Scanner sc = new Scanner(System.in);
    System.out.print("Enter Date: ");
    String ind = "09/05/2011"; //sc.nextLine();
    DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
    Date d = null;
    try {

    d=df.parse(ind);

    }
    catch(ParseException e) {
    System.out.println("Unable to parse " + ind);}


    DateFormat df3 = DateFormat.getDateInstance();


    String s3 = df3.format(d);


    System.out.println("The entered date is: " + s3);

    }
    }

    and the output was:

    Enter Date: The entered date is: Jan 5, 2011

    Process completed.

  18. #18
    Shahnawaz is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default Re: Java Date input from User,

    you were right junky; it worked.. thanks again guys :)

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Java Date input from User,

    Huh?

    You didn't do as I suggested.

  20. #20
    Shahnawaz is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default Re: Java Date input from User,

    i did... that was what norm suggested; here's the code

    import java.util.*;
    import java.text.*;
    public class wy2 {
    public static void main(String[] args) {


    Scanner sc = new Scanner(System.in);
    System.out.print("Enter Date: ");
    String ind = sc.nextLine();
    DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
    Date d = null;
    try {

    d=df.parse(ind);

    }
    catch(ParseException e) {
    System.out.println("Unable to parse " + ind);}


    DateFormat df3 = new SimpleDateFormat("mm/dd/yyyy");


    String s3 = df3.format(d);


    System.out.println("The entered date is: " + s3);

    }
    }

    and the output..

    Enter Date: 12/23/2011
    The entered date is: 12/23/2011

    Process completed.

Page 1 of 2 12 LastLast

Similar Threads

  1. Taking user input as a date and validation!
    By Junki3JJC in forum New To Java
    Replies: 3
    Last Post: 03-29-2011, 02:28 PM
  2. Replies: 5
    Last Post: 03-23-2011, 11:28 PM
  3. Replies: 6
    Last Post: 03-01-2010, 08:00 AM
  4. How do I validate user input from forms with Java?
    By rickywh in forum New To Java
    Replies: 2
    Last Post: 01-30-2010, 07:49 AM
  5. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 09:10 AM

Posting Permissions

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