Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to remove Date.getYear() deprecation warnings ...

Status
Not open for further replies.

djmnyc

Programmer
Oct 23, 2003
25
US
Hello.

I have a Timestamp field I am obtaining from my DB. I need to format this field in CCYYMMDD format. This is not my problem. I extract the year, month, and day fields from the Timestamp instance using the Timestamp (inherited from superclass Date) getYear(), getMonth(), and getDate() functions.

However, these methods are giving me deprecation warnings. When consulting the JDK API, I see that the use of Calendar.get(Calendar.YEAR) - 1900 is recommended for obtaining the year, and similar Calendar get() calls for the other fields.

However, there is no constructor for the concrete class GregorianCalendar given a Date or Timestamp object. In order to construct an instance, I must provide the year, month, and day values in the constructor. This does me no good. Am I missing something here?

How can I retrieve the year, month, and day values from a Timestamp instance without getting deprecation warnings? That is the question.

Thanks everyone.
Dan
 
How about:
Code:
import java.util.*;
public class Test {
    public static void main(String[] args) throws Exception {
        Date t = new Date();
        Random r = new Random();
        Thread.sleep(r.nextInt(1000));
        Calendar c  = new GregorianCalendar();
        c.setTime(t);  // this sets the Calendar instance based on your timestamp
        System.out.println(c.get(Calendar.YEAR));
    }
}
Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top