This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java util Date & Calendar - 1900 years

 
anagha patankar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,

I am creating a calendar object and am setting values such as :
year , date , month , hour , minute and seconds .

Here is the code :


The output that is generated is :


I dont quite understand whay the "Date" object adds 1900 years while creating the date object .

Could some one shed some light .
Regards,
-anagha
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15367
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the API documentation for the the constructor of class Date that you are using. A quote:

Parameters:
year - the year minus 1900.
...


And that constructor is deprecated, which means you should not use it. If you want a Date object that has the same date and time as the Calendar, just use the getTime() method. You already know that method, because you're using it in your code above.
 
anagha patankar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper for the response .

However one point that I dont uite understand is :
Though I am explicitly setting hour and minute in calendar to 23:00

when I use the calendar's method : "getTime " I get :
cal.getTime = Wed Jun 21 20:30:00 IST 2006

In my application the hour is critical for logic
and this totally throws the system off.

Dont understand why the hour and minutes are changing ?

Thanks ,
-anagha
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a timezone problem. You are creating your calendar with the timezone CCT, but the time is printed in IST.
[ July 27, 2006: Message edited by: Scott Johnson ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15367
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The time zone stuff can be difficult to handle in Java.

The Date class does not know anything about time zones. If you read the API documentation of class Date, it says it is intended to contain a Date in UTC (coordinated universal time).

If you print a Date to the console by just using toString() on the Date object (you are doing that implicitly in your code above), it prints the Date in the time zone of your default locale.

If you want to convert a Date to text using a specific time zone, you can use a DateFormat object and set the time zone on the DateFormat object:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic