0

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??

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by jrosh
0

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

0

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...

0

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..

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.