Solved

How to create a Calendar object from a Date object?

Posted on 2008-10-01
9
1,520 Views
Last Modified: 2008-12-09
Hi,
I have a java.util.Date object created from the ResultSet query as:


Date createDate = resultSet.getDate(QueryManager.SCHEMA_CCI_CONTACT_COLUMN_CREATED_DATE);


Now I need to set a Calendar date variable of a pojo instace c as:

c.setCreateDate(new GregorianCalendar(createDate.getYear(),      createDate.getMonth(), createDate.getDate())); //old code - deprecated


Now

createDate.getYear(),      
createDate.getMonth(),
createDate.getDate()

are deprecated in Java 5

How best can I modify the code to achieve the same removing the deprecated Date methods?



I can do it like this:

Date createDate = resultSet.getDate(QueryManager.SCHEMA_CCI_CONTACT_COLUMN_CREATED_DATE);
                        
Calendar createDateCal = Calendar.getInstance();
createDateCal.setTime(createDate);


if(createDate!=null){
      c.setCreateDate(new GregorianCalendar(createDateCal.get(Calendar.YEAR),      createDateCal.get(Calendar.MONTH), createDateCal.get(Calendar.DATE)));      
}

Does it do the same thing as above?

Is there any better option to acheive the same ...?

Let me know.

Thanks.
_Esam.
0
Comment
Question by:_Esam
[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
  • 3
  • 3
9 Comments
 
LVL 5

Expert Comment

by:libin_v
ID: 22617722
Please find teh equivalents below

getDate() = Calendar.get(Calendar.DAY_OF_MONTH)
getMonth() = Calendar.get(Calendar.MONTH)
getYear() = Calendar.get(Calendar.YEAR) - 1900
0
 
LVL 8

Expert Comment

by:mnrz
ID: 22617728
Calendar c = Calendar.getInstance();
c.setTime(createDate);

0
 
LVL 8

Accepted Solution

by:
mnrz earned 50 total points
ID: 22617768
if c.setCreateDate(..) accepts a calendar as parameter so you can simply set

c.setCreateDate(createDateCal);

0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:_Esam
ID: 22617940
The pojo c set the Calendar date as:

public void setCreateDate(Calendar createDate) {
  this.createDate = createDate;
}

So it does accept a Calendar object as parameter.
So in this case, I can simply set it as you said:
c.setCreateDate(createDateCal);

whereas I would have to create the createDateCal as I mentioned:

Date createDate = resultSet.getDate(CREATED_DATE);
                       
Calendar createDateCal = Calendar.getInstance();
createDateCal.setTime(createDate);

Is that ritght?

Thanks.
_Esam.
0
 
LVL 8

Expert Comment

by:mnrz
ID: 22618035
yes it's ok
0
 

Author Comment

by:_Esam
ID: 22618049
Ok.... Thanks much ..

_Esam..
0
 

Author Comment

by:_Esam
ID: 22618053
It works for me ... thax...

_Esam..
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

734 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