Solved

How to create a Calendar object from a Date object?

Posted on 2008-10-01
9
1,513 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
  • 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

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

Suggested Solutions

Title # Comments Views Activity
changePi Challenge 15 77
groupSum6 challenge 6 45
create a gui in perl 3 46
Java passing arguments (type error) 2 35
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.

762 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

21 Experts available now in Live!

Get 1:1 Help Now