File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Is java Date/Calendar a bug? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is java Date/Calendar a bug?" Watch "Is java Date/Calendar a bug?" New topic
Author

Is java Date/Calendar a bug?

Li Shangqiang
Greenhorn

Joined: Mar 25, 2001
Posts: 22
I used java.util.Calendar like this:
Calendar calendar = new GregorianCalendar(2001, 6, 30);
calendar.add(Calendar.DATE, 1);
//or calendar.add(Calendar.DATE, 1);
//or calendar.add(Calendar.HOUR, 24);
System.out.println("year: "+calendar.get(Calendar.YEAR)+
"\n month: "+calendar.get(Calendar.MONTH)+
"\n day: "+calendar.get(Calendar.DAY_OF_MONTH)+
"\n hour: "+calendar.get(Calendar.HOUR)+
"\n minute: "+calendar.get(Calendar.MINUTE)+
"\n sencond: "+calendar.get(Calendar.SECOND)
);
the result is always 2001-6-31!
then i use java.util.Date:
Date date = new Date(2001, 6, 30);
long current = date.getTime();
current += 24 * 3600 * 1000; //add one day
System.out.println(..
and the result is the same!
who answer me?
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
There is no problem with the java.util.date object.
If you try to convert the time back into the date object you would see that it prints the next date.

Date date = new Date(2001, 6, 30);
long current = date.getTime();
current += 24 * 3600 * 1000; //add one day
Date newDate = new Date(current);
System.out.println(" date " + date);
System.out.println(" newDate " + newDate);

Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
If you are referring to 6-31 and thinking it is June 31 you are mistaken. Java months start at 0 for Jan. So 6-31 is Jul 31.
Li Shangqiang
Greenhorn

Joined: Mar 25, 2001
Posts: 22
Yes, that's the problem.
I'v got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is java Date/Calendar a bug?
 
Similar Threads
i want to get the month of a particular date
sql syntax in a servlet
milliseconds
How to compare (Hour) with actual new Date() ?
add one day to current date