General Question

knight's avatar

Using Java Date class...

Asked by knight (35points) February 29th, 2008

Hi,
I want to use deprecated java date class.
When I run:

Date d1 = new Date(108,2,19); System.out.print(d1.getDate()+”/”+d1.getMonth()+”/”+d1.getYear());

I get 19/2/108. But I expect to get the year 2008 like written in
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html#Date(int,%20int,%20int)

My question is: how can I get the year 2008 with deprecated date class?
I mean give 108 in constructor and get 2008.

Thanks in advance Arshavski Alex.

Observing members: 0 Composing members: 0

1 Answer

dibau_naum_h's avatar

Date is deprecated because it isn’t locale aware. It was replaced with the Calendar family of classes.
In order to create & populate an object representing a date in Java you can do something like:
Calendar c = new GregorianCalendar(2008, Calendar.FEBRUARY, 19);
String s = DateFormat.getDateInstance(DateFormat.SHORT).format(c.getTime());

or more generally:
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, 2008);
c.set(Calendar.MONTH, Calendar.FEBRUARY);
c.set(Calendar.DATE. 19);

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther