Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Error after trying to convert Date into String

    I've been trying so hard to convert the date into a string and this is the closest I've gotten i think.
    Java Code:
    SpeedDating thanksgiving = new SpeedDating() ; 
    
    
    
    input = JOptionPane.showInputDialog
    			           ("Enter the year for november ") ;
           
           int year = Integer.parseInt(input) ;
           
           
          java.util.Date dateNow = new java.util.Date();
    
    
    SimpleDateFormat formatDateJava = new SimpleDateFormat("dd/MM/yyyy");
    String date_to_string = formatDateJava.format(thanksgiving.getThanksgiving(year));
    System.out.println("Today's date into dd/MM/yyyy format: " + date_to_string);
    The error shows up after running the program. It says, "Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date". What's going on? How can I fix it?

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

    Default Re: Error after trying to convert Date into String

    What is the full text of the error message that shows the line number where the error happens?

    What does the getThanksgiving() method return?
    What type of argument does the format method take?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Error after trying to convert Date into String

    I'm sorry. It supposed to return a date in the form (month, day, year). The year is inputted by the user.

    Java Code:
    Date Thanksgiving = new Date(10, 1, year);
           
            int count = 1 ;
            int weekCount = 0;
            for (  ; count <= 30; count++)
            {
                
                if(Thanksgiving.getDay()==4) {
                    weekCount++;
                }
                if (weekCount == 4) {
                    break;
                }
               
                Thanksgiving=new Date(10, count, year);
    The format method? You mean the "formatDateJava.format" part? I'm trying to print out the date.

    When I had this
    Java Code:
    SpeedDating thanksgiving = new SpeedDating() ;
     
     
    input = JOptionPane.showInputDialog
                                       ("Enter the year for november ") ;
           
           int year = Integer.parseInt(input) ;
         
           
           System.out.println(thanksgiving.getThanksgiving(year)) ;
    This prints out something like Date@1762027 which I was told was a reference "memory location". I need to convert that into a string.

  4. #4
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Error after trying to convert Date into String

    I asked a similar question to this in a previous post. I was told to use simpledateformat but I I don't know :(

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

    Default Re: Error after trying to convert Date into String

    Did you see the questions I asked in my post? Can you answer them?

    What does the getThanksgiving() method return? What type of data does the method return?
    What type of argument does the format() method take? Read the API doc to see for sure.

    Date@1762027
    That looks like what the toString() method would return for the class: Date.
    Do you have your own version of the Date class? If so, add a toString() method to it that returns the contents of the class in a String in a useful format.
    If you have your own class named Date, that is a problem because Java SE has a class with the same name and that will be confusing for anyone reading your code. I recommend changing the class's name.

    I'm done for tonight. Back tomorrow. Some one else could come after this.
    Last edited by Norm; 11-05-2012 at 05:03 AM.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    0

    Default Re: Error after trying to convert Date into String

    Thank you. d1.getShortDate() // returns the date as mm/dd/yyyy
    How would I use that? So something like System.out.println(thanksgiving.getShortDate()); But I need to access the date that is stored in Thanksgiving which is in the other class. How would I do that?

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

    Default Re: Error after trying to convert Date into String

    How would I use that? So something like System.out.println(thanksgiving.getShortDate());
    What happens when you try that in the code?

    To access data in another class, you can call a method in that class and have it return the data.

    Is this related?
    http://www.javaprogrammingforums.com...y-methods.html
    Last edited by Norm; 11-05-2012 at 10:24 PM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. convert string to a date
    By exose in forum New To Java
    Replies: 10
    Last Post: 03-14-2011, 01:12 PM
  2. Convert String to Date?
    By bochra in forum New To Java
    Replies: 4
    Last Post: 11-15-2010, 11:41 AM
  3. convert String to date and pass to PreparedStatement
    By computerbum in forum New To Java
    Replies: 1
    Last Post: 09-21-2010, 04:31 AM
  4. convert String date to Date
    By computerbum in forum New To Java
    Replies: 7
    Last Post: 09-18-2010, 04:26 PM
  5. how we can convert string to date?
    By cowboy2010 in forum New To Java
    Replies: 4
    Last Post: 08-25-2010, 09: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
  •