Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Issue with converting a string to a date

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    8
    My Mood
    Amused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Issue with converting a string to a date

    I have this section:
    	public void set_doh(int new_m, int new_d, int new_y)
    	{
    		String datestring = Integer.toString(new_m) + "/" + Integer.toString(new_d) + "/" + Integer.toString(new_y);
    		System.out.println(datestring);
    		DateFormat DOB = new SimpleDateFormat("m/d/yyyy");
     
    		try
    		{
    			Date convertedDate = DOB.parse(datestring);
    			this.doh = convertedDate;
    		}
    		catch (ParseException e) {
    			System.out.println("Failed to parse");
    		}
    	}
    which is passed information from here:
    employee1.set_doh( Integer.parseInt(scanner.nextLine()),Integer.parseInt(scanner.nextLine()),Integer.parseInt(scanner.nextLine()) ); // int
    The datestring is printed in the correct format in the first block but when it goes to convert it into Date format I get:
    E:\Java Project\Employee.java:145: error: incompatible types
    			Date convertedDate = DOB.parse(datestring);
    			                              ^
      required: Date
      found:    java.util.Date
    1 error

    Now, I have a Date.java class in the same folder which was given to me by my instructor and I'm supposed to use that in conjunction with a Dateutil.java she gave me as well. I've noticed that if I remove Date.java from the folder this section of the code will work but then I cannot use the Dateutil.java properly, which is necessary to compare the current date with the object's date.

    Should I post the all the code in all three files?

    Thanks for any and all help on this!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,372
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Issue with converting a string to a date

    Try putting the full package path on the class name so the compiler does not get confused about class definitions: java.util.Date

    Which Date class is convertedDate supposed to be?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    8
    My Mood
    Amused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Issue with converting a string to a date

    Hi Norm,

    I actually tried that but there are other lines which compare convertedDate to objects represented in the Date class, so they have to be compatible at some point.

    The program is supposed to take the date as the three separate integers from the text file and set them into the employee object. However, they are meant to be made into a proper Date format because I am then supposed to use the date to calculate the employee's years of service.

    So to answer your question convertedDate is supposed to be the Date class. The program I assume needs to convert from java.util.Date to Date, or perhaps I need to write the parse() method into the Date class I was provided? Do you know how I could do that?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,372
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Issue with converting a string to a date

    convertedDate is supposed to be the Date class.
    If convertedDate is the local Date class, then you can not assign it the value returned by the parse() method which returns a java.util.Date object.
    The program I assume needs to convert from java.util.Date to Date
    Yes, that seems right.
    What methods does the java.util.Date class have that can be used to create a local Date class object?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    8
    My Mood
    Amused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Issue with converting a string to a date

    Quote Originally Posted by Norm View Post
    What methods does the java.util.Date class have that can be used to create a local Date class object?
    I feel like I should be the one asking this question!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,372
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Issue with converting a string to a date

    I don't have a definition for the local Date class. You have to read the code or the API doc for that class to see how to use it.
    How can anyone make recommendations for using an unknown class?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    8
    My Mood
    Amused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Issue with converting a string to a date

    This is all it contains:
    public class Date
    {
          private int month;
          private int day;
          private int year;
     
       Date()
       {
       }
       Date(Date aDate)
       {
          month = aDate.month;
          day = aDate.day;
          year = aDate.year;
       }
     
       Date(int m, int d, int y)
       {
    		month = m;
    		day = d;
    		year = y;
       }
     
       public int get_month()
       {
          return month;
       }
     
       public void set_month(int new_month)
       {
          month = new_month;
       }
     
       public int get_day()
       {
          return day;
       }
     
       public void set_day(int new_day)
       {
          day = new_day;
       }
     
       public int get_year()
       {
          return year;
       }
     
       public void set_year(int new_year)
       {
          year = new_year;
       }
     
    } // end Date class

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,372
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Issue with converting a string to a date

    Looks like you need to create a Calendar object, set its time using the Date object, then call the Calendar object's get method to get the year, month and day to create the local Date object.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2012
    Posts
    8
    My Mood
    Amused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Issue with converting a string to a date

    			java.util.Date convertedDate = DOB.parse(datestring);
    			Calendar calendar = Calendar.getInstance();
    			calendar.setTime(convertedDate);
    			this.doh = calendar.getTime();
    Same error.

  10. #10
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: Issue with converting a string to a date

    There are two ways you can do it with relative ease.

    One way that *should* work, but may not be what your instructor prefers you to do is by saying:
    ...
    java.util.Date convertedDate = DOB.parse(datestring);
    Date dateObject = new Date(...); /*send the arguments for the month, day, and year by calling the convertedDate.getYear(), ect. methods. NOTE: this will draw a handful of warnings since the java.util.Date class has been more or less Deprecated (Calendar is the more widely used one now)*/
    ...

    Alternatively, you can do what I expect your instructor to *want* you to do, which would be just using the values sent to the set_doh(int,int,int) method. Why are you even creating the datestring and parsing the values back out? Why not just use the int values you used to create the datestring?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  11. The Following User Says Thank You to aussiemcgr For This Useful Post:

    dave_nj (May 2nd, 2012)

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,372
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Issue with converting a string to a date

    Same error.
    What was the text of the error?
    What datatype is doh? Does the Calendar class's getTime() method return that type of data?

    You need to use the Calendar class's get() method to get the year, month and day to use with set method on the local Date class object. See the Date class's getMonth() method API doc for how to code the get method.
    Last edited by Norm; May 2nd, 2012 at 01:59 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #12
    Junior Member
    Join Date
    May 2012
    Posts
    8
    My Mood
    Amused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Issue with converting a string to a date

    aussiemcgr,

    I actually added the
       Date(int m, int d, int y)
       {
    		month = m;
    		day = d;
    		year = y;
       }
    to the Date class, it wasn't there before.

    I've tried your alternate suggestion previously and when I print the dates back they come out as "Date@kdfj23" or some such unusable muck. Which is why I have been trying to find another way to do it.

    I've just realized that maybe I simply need to convert that "Date@kdfj23" back into a more presentable form.

    Is this the case?

  14. #13
    Junior Member
    Join Date
    May 2012
    Posts
    8
    My Mood
    Amused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Issue with converting a string to a date

    Norm,

    The text of the error is the same as originally,
    required: Date
    found: java.util.Date

    doh is "Date"

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,372
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Issue with converting a string to a date

    come out as "Date@kdfj23" o
    That is what is returned by the class's default toString() method. You should override the toString method and have it return the String you want to see.
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    dave_nj (May 2nd, 2012)

  17. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,372
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: Issue with converting a string to a date

    required: Date
    found: java.util.Date
    See post#11.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #16
    Junior Member
    Join Date
    May 2012
    Posts
    8
    My Mood
    Amused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Issue with converting a string to a date

    Norm, aussiemcgr,

    Thanks for the help, especially the tip about toString!
    I should have mentioned the string the program was giving me in the OP, would have saved us some time haha.

    Dave

Similar Threads

  1. Help with code for converting 4 digit string to integer
    By danielp1213 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2011, 09:38 PM
  2. Replies: 1
    Last Post: July 22nd, 2011, 07:08 AM
  3. how to parse this string to date ?
    By vaibhav in forum Member Introductions
    Replies: 1
    Last Post: March 11th, 2011, 03:17 PM
  4. Converting to String
    By darek9576 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 13th, 2010, 05:09 PM
  5. Replies: 2
    Last Post: November 3rd, 2009, 05:28 AM