wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Java util Date & Calendar - 1900 years Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java util Date & Calendar - 1900 years" Watch "Java util Date & Calendar - 1900 years" New topic
Author

Java util Date & Calendar - 1900 years

anagha patankar
Ranch Hand

Joined: Dec 26, 2005
Posts: 53
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

Joined: Aug 16, 2005
Posts: 14192
    
  20

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
anagha patankar
Ranch Hand

Joined: Dec 26, 2005
Posts: 53
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

Joined: Aug 24, 2005
Posts: 518
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

Joined: Aug 16, 2005
Posts: 14192
    
  20

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:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Java util Date & Calendar - 1900 years