开发者

In java, Date() method returns a value. but I am confused in that

开发者 https://www.devze.com 2022-12-22 06:24 出处:网络
I used Date() for getting the date of my birthday, but it was retu开发者_开发问答rned the mismatch of the month. My birthday is 04-March-87. so i gave an input as,

I used Date() for getting the date of my birthday, but it was retu开发者_开发问答rned the mismatch of the month. My birthday is 04-March-87. so i gave an input as,

Date birthDay= new Date(87,03,04,8,30,00);

But it returns correct year, day and time. But month was problem. It displays April month.

What's wrong with that?


Months are set from 0 to 11, January =0, February = 1, ..., December = 11.

So, for April do this:

Date birthDate = new Date(87,02,04,8,30,00); //March = 2

Hope this helps;

EDIT

The Date class with this constructor public Date(int year, int month, int date, int hrs, int min) is deprecated (i.e. it has been declared @Deprecated).

Rather do this.

Calendar calendar = Calendar.getInstance();
Calendar.set(1987, 2, 4, 0, 0, 0);
Date birthDate = calendar.getTime();

(It will return the same thing as what you asked for)


The month in the Date class starts with 0 for January, so March is 2, not 3.

Also, the Date(int, int, int, int, int, int) constructor is deprecated, so you should consider using the Calendar class instead.

Finally, be careful with leading zeros in Java - they represent octal constants. The number 09 would not do what you expect.

0

精彩评论

暂无评论...
验证码 换一张
取 消