• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding date and time to a class?

 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm currently writing a number of classes to handle video loans etc. The current class, called 'Loans', handles simple information about a loan of a video to a borrower.

My question is simply how do I add the date and time to this class so it is represented with the loan information for a borrower?

My code for this class is currently as follows,



Basically, when I call the toString method I need to be able to print out the date and time as well. Any help really appreciated!!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by celine scarlett:
... Basically, when I call the toString method I need to be able to print out the date and time as well. Any help really appreciated!!

Sounds like you might want to take a look at the java.util.Date class...
 
Scott Selikoff
author
Saloon Keeper
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or the more formal Calendar class:



This was created as a replacement to many of the deprecated method of the Date class.
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

That's great. Thank you for your help!!
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as an aside, and in my opinion, using a Calendar to represent Dates is not ideal. An instance of Calendar is useful for manipulating Dates, and those are the methods of Date that have been deprecated. As such, I never have (meaningful) class member veriables of type Calendar -- to me, that is just a utility class. To store Dates, just using Date is all that is needed.

In short:

Representing dates: java.util.Date (or subclasses java.sql.Date or java.sql.Timestamp
Manipulating dates: java.util.Calendar
Printing Dates: java.text.DateFormat (usually the java.text.SimpleDateFormat)
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see where a Date can suffice, but remember that a Date doesn't
include TimeZone state, while a Calendar does. So if you are replacing
a Calendar by a Date, you should include a TimeZone, or at least its time
offset.

This is of course only in contexts where you care about time zones!
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, but dates are stored in GMT -- when you print them out, they are automatically converted to your time zone. So a date of 0 milliseconds will print out on the US East Coast as Dec 31, 1969 at 7:00 pm -- because that is 1/1/1970 0:00 GMT. So in order to see what that time is in different timezones, you need to use the Calendar (or change the timezone on your computer). Again, Calendar for manipulation, Date for storing. It's not that often that you actually need to store June 20, 1976 at 10:00 pm EDT -- simply storing the universal time of June 21, 1976 02:00 GMT will suffice and will be converted accordingly.

Dates represent a point in time -- they are universal. In general, this is all that you need.

(This is just me continuing my rant on the overuse of Calendars, since it has been my experience that people don't recognize this fact about Dates...)
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been wanting to try Joda-Time for some time now. You may want to jave a look at tha. Using java util Date/Calender, especially when doing a lot of Date mathematics can get very messy.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joel McNary:
So in order to see what that time is in different timezones, you need to use the Calendar (or change the timezone on your computer).


Your point is well-taken. I realize now that I am a calendar abuser!

But I do have a small point to make about the above statement. I assume that to
"see" what the time is, is referring to formatting the date as a string.
While you can change the default timezone in your JVM by calling TimeZone.setDefault(timeZone)
(if you really want to do that!), the cleaner way is to use a DateFormat
and explicitly set its TimeZome property:

Note that I'm using no Calendars! DateFormat's API doesn't mention Calendar,
which can be a source of bugs in code, because you need to extract both
Date and TimeZone from Calendar and pass them separately to DateFormat for
it to render the Calendar in the calendar's time zone.
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks to everyone for their helpful and detailed replies. It has all be most useful and informative.

Many thanks!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic