Solved

How to create a Calendar object from a Date object?

Posted on 2008-10-01
9
1,518 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 42
print map entry 34 72
Windows 10 IE Certificate Issue 10 51
Running JavaFX on the Raspberry Pi 27 40
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 contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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 …

861 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