Solved

Date convertion

Posted on 2003-12-10
8
1,355 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 100 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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Display the newly inserted values 3 83
Help with LDAp login using jsp 6 118
difference between member and local variables. 29 144
console vs log file 16 83
While it may be true that the internet is a place of possibilities, it is also a hostile environment lurking with many dangers. By clicking on the wrong link, trusting the wrong person or using a weak password, you are virtually inviting hackers to …
This article discusses how to implement server side field validation and display customized error messages to the client.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 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