?
Solved

Date convertion

Posted on 2003-12-10
8
Medium Priority
?
1,358 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:fks2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
8 Comments
 
LVL 3

Expert Comment

by:applekanna
ID: 9918070
1.
util_date = df.parse(str_date)

0
 
LVL 3

Expert Comment

by:applekanna
ID: 9918125
2.

dateString = df.format(util_date);
0
 
LVL 3

Accepted Solution

by:
applekanna earned 400 total points
ID: 9918128
 /**
   * 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());
    try
    {
      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());
       try
       {
         utilDate = new java.util.Date(sqlDate.getTime());
       }
       catch(NullPointerException e){
           utilDate = null;
       }

       return utilDate;
     }
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:applekanna
ID: 9918131
hope this helps
Cheers!
0
 

Author Comment

by:fks2
ID: 9918796
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?




0
 
LVL 3

Expert Comment

by:applekanna
ID: 9922111
>>CAN we ignore this???

Yes you can

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

This is a util date

Hope this helps
Chers
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9925503
>> 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;
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9926433
thx for the pointx :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn how to use the free Acronis True Image app to easily transfer data between iPhones and Android phones.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question