wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Adding date and time to a class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding date and time to a class?" Watch "Adding date and time to a class?" New topic
Author

Adding date and time to a class?

celine scarlett
Ranch Hand

Joined: Nov 06, 2005
Posts: 93
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

Joined: Aug 31, 2004
Posts: 11343

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...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3700
    
    5

Or the more formal Calendar class:



This was created as a replacement to many of the deprecated method of the Date class.


My Blog: Down Home Country Coding with Scott Selikoff
celine scarlett
Ranch Hand

Joined: Nov 06, 2005
Posts: 93
Hi,

That's great. Thank you for your help!!
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

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)


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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!


There is no emoticon for what I am feeling!
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

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

Joined: Feb 02, 2002
Posts: 1211

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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Nov 06, 2005
Posts: 93
Hi,

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

Many thanks!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding date and time to a class?
 
Similar Threads
Errors when reading and writing from a text file - any help!!
manager class with testing - any help please!!!
Creating a new object from a text file - please help!!
Creating a pointer/reference to another object - any help!!
Error in run method and class - help!