Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to create a Calendar object from a Date object?

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

c.setCreateDate(createDateCal);

0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

670 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