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!!
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...
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)
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
This is of course only in contexts where you care about time zones!
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...)
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.