Solved

Date convertion

Posted on 2003-12-10
8
1,339 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
  • 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
 
LVL 3

Expert Comment

by:applekanna
ID: 9918131
hope this helps
Cheers!
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now