• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is java Date/Calendar a bug?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's the problem.
I'v got it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic