Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to create a Calendar object from a Date object?

Posted on 2008-10-01
9
Medium Priority
?
1,528 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 200 total points
ID: 22617768
if c.setCreateDate(..) accepts a calendar as parameter so you can simply set

c.setCreateDate(createDateCal);

0
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!

 

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

Independent Software Vendors: 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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

963 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