I used below code to extract year from a date object.

java.util.Date ye = new java.util.Date();  
           		int y = ye.getYear();
           		       		
           		System.out.println(y);

But it prints ,109

How I get the year as 2009??

Recommended Answers

All 3 Replies

Don't use these methods:
ye.getYear()
They are deprecated.
Instead use the java.util.Calendar
And use the get method

Calendar rightNow = Calendar.getInstance();
rightNow.get(Calendar.MONTH);

This will return the month. Read the static fields of the API

Specifically, you should use:

Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);

Alternately, if you need the date formatted in various strings, you should use the java.text.SimpleDateFormat class. Google it and the Javadoc page has lots of details about how to use it...

Specifically, you should use:

Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);

Alternately, if you need the date formatted in various strings, you should use the java.text.SimpleDateFormat class. Google it and the Javadoc page has lots of details about how to use it...

Thanx..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.