Date convertion

hi, this is a simple question, but yet i get confused all the time and i need clarification.

I have this code,

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date util_date = new java.util.Date();
String str_date = "11/05/2003";

1. how can i convert  a String's date (str_date) to a util date.
2. how can i convert a util date to String date?
3. how can i convert Sql's date to util's date?
4. how can i convert  util's date to sql's date?

5. I have the below code, and i have problem.
- i have a String date, ida
- i need to add terms on the ida based on the a_term
- I am using Calenda interface
- i stuck at the convertion from String to util date.

String ida="12/12/2003";
Calendar f_a  = Calendar.getInstance();
String a-term="12";

//method deprecated :
//java.util.Date i_da= new java.util.Date(ida);

//Can not resovle sympol, getInstance()
java.util.Date i_da = new java.text.DateFormat.getInstance().parse(ida);

f_a.add(Calendar.MONTH, Integer.parseInt(a_term));

thank you.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

util_date = df.parse(str_date)


dateString = df.format(util_date);
   * A general method to give java.sql.Date given a java.util.Date
  public static java.sql.Date utilDate2SqlDate(java.util.Date utilDate)
    java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
      sqlDate = new java.sql.Date(utilDate.getTime());
    catch(NullPointerException e){
        sqlDate = null;

    return sqlDate;

  }//end utilDate2SqlDate

   * A general method to give java.util.Date given a java.sql.Date
  public static java.util.Date SqlDate2UtilDate(java.sql.Date sqlDate)
       java.util.Date utilDate = new java.util.Date(System.currentTimeMillis());
         utilDate = new java.util.Date(sqlDate.getTime());
       catch(NullPointerException e){
           utilDate = null;

       return utilDate;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

hope this helps
fks2Author Commented:
tq. let me try and get back to you.

util_date = df.parse(str_date)

public Date parse(String text,
                  ParsePosition pos)Overrides DateFormat
parse in class DateFormat
See Also:

applekanna, i saw this in the documentation. but i wonder how about the ParsePosition that require us to pass in along with the Stirng test at the header? CAN we ignore this???

I understand that this will return me a Date, but this date is it util.Date or sql.Date?

>>CAN we ignore this???

Yes you can

>>is it util.Date or sql.Date?

This is a util date

Hope this helps
>> i saw this in the documentation
this is another method in super class:
public Date parse(String text)

util.Date to sql.Date: applekanna is correct on this, basically you need:
sqlDate = utilDate==null? null : new java.sql.Date( utilDate.getTime() );
sql.Date to util.Date: sql.Date is subclass of util.Date, you can just assign:
utilDate = sqlDate;
thx for the pointx :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.