Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Date convertion

Posted on 2003-12-10
8
Medium Priority
?
1,362 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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!

Question has a verified solution.

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

An overview of cyber security, cyber crime, and personal protection against hackers. Includes a brief summary of the Equifax breach and why everyone should be aware of it. Other subjects include: how cyber security has failed to advance with technol…
Here in this article, you will get a step by step guidance on how to restore an Exchange database to a recovery database. Get a brief on Recovery Database and how it can be used to restore Exchange database in this section!
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

609 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