Solved

Date convertion

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

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
We take a look at some of the most common obstacles that IT teams run into as they work relentlessly to keep all the alarms and sirens from going off at once.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 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