• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Java util Date & Calendar - 1900 years

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Good night. Drive safely. Here's a tiny ad for the road:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic