Link to home
Start Free TrialLog in
Avatar of fks2
fks2

asked on

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.setTime(i_da);
f_a.add(Calendar.MONTH, Integer.parseInt(a_term));

thank you.
Avatar of applekanna
applekanna

1.
util_date = df.parse(str_date)

2.

dateString = df.format(util_date);
ASKER CERTIFIED SOLUTION
Avatar of applekanna
applekanna

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hope this helps
Cheers!
Avatar of fks2

ASKER

tq. let me try and get back to you.

util_date = df.parse(str_date)

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

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
Chers
>> 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 :)