Date vs Calender

B

B J H

Hi,

I've been aksed to write a program to simulate a taxi booking system. The
user inputs the date/time of the pickup, the pick-up location, driver name,
and the car number (there are only 5 cars).

The system must validate the date/time entered for the pick up. I.e. it must
be a valid date and must not be in the past. Also, each job is presumed to
last 30 minutes, and when booking a new job, the system must check the car
number, see whether its already booked, check if 30 mins have passed since
the pick up time, and if so it can be booked again.

I am really struggling implementing this. Should I use date or calender? Or
is there an easier way to do this ?


TIA
 
T

Thomas Weidenfeller

B J H said:
I am really struggling implementing this.

Take a sheet of paper.

List all date/time operations you have to do.

Also list all I/O operations you have to do which involve date/time.

Consider your storage requirements (how many date/time information do
you have to store for what time?).

For each of the operations check the API documentation of Date,
Calendar, SimpleDateFormat etc, if the API provides the exact operation
or a simple way to get the desired behavior by combining method calls
or subclassing.

Chose that class or primitive type as the type for representing your
data that provides the most of the desired operation. In case you have
specific storage requirements, decide when to convert data to some
simple format, e.g. to an int.

For non-supported operations develop the necessary algorithms, or
search books and the web for implementations or algorithms.

Decide on or develop the necessary data structures (collections) to
hold your bunch of data. Again, think about the requiret operations and
algorithms.
Should I use date or calender?

You decide.
Or is there an easier way to do this ?

Also consider using simple int timestamps.

Think about geting some book about object-oriented analysis and
design for a more streamlined abroach than the one sketched above.

/Thomas
 
B

B J H

The problem being this is due in tomorrow at 11am and I've not even started
implementing any time features. Our lecturer has not told us anything about
Date/Calender classes or how to use them.


I'm thinking of just using a string to represent the time like "
 
B

B J H

I've decided to use Calender, and there doesn't seem to be an easy way to
set the time using Time, but I can pass ints to the Calendar.

Still when I try I'm getting lots of exceptions. I'll keep trying..
 
M

Michael Borgwardt

B said:
I've decided to use Calender,

Wrong choice. In your requirements, I see nothing that requires the use
of Calendar. Use Date instances to represent a particular moment in time.
Use java.text.SimpleDateFormat to convert between the Date instances and
the Strings the user enters and is shown. To calculate the time difference
between two Dates, call their getTime() methods, subtract the results
and divide it by 1000 to get seconds (and further, as necessary).

Calendar is only necessary for converting between Date instances and
"parts" of them (i.e. day of the month, etc.) and use used internally
by SimpleDateFormat.
 
B

B J H

Thank you for your reply!

I'll have a go ;)

Michael Borgwardt said:
Wrong choice. In your requirements, I see nothing that requires the use
of Calendar. Use Date instances to represent a particular moment in time.
Use java.text.SimpleDateFormat to convert between the Date instances and
the Strings the user enters and is shown. To calculate the time difference
between two Dates, call their getTime() methods, subtract the results
and divide it by 1000 to get seconds (and further, as necessary).

Calendar is only necessary for converting between Date instances and
"parts" of them (i.e. day of the month, etc.) and use used internally
by SimpleDateFormat.
 
B

B J H

I'm trying to pass a date to a method using the simpledateformat parser but
it wont work.

"cannot resolve symbol - constructor Date(java.util.Date)"

I'm doing it as follows.

Here's the method which expects date to be passed to it...

//constructor which takes the data and creates an instance of Booking
public Booking(String name, int carno, String location, String destination,
Date date)
{
DriverName = new String(name);
carno = CarNumber;
PickUpLocation= new String(location);
DropOffLocation = new String(destination);
PickUpDate = new Date(date);
}


And here's the method call...

//create date object using SimpleDateFormat and pass over the date/time
pattern string
SimpleDateFormat dateformat = new
SimpleDateFormat("ddMMyyyyHHmm");

dateEntered = new Date(dateformat.parse(datetime));

//create new booking passing over the required parameters
Booking t = new Booking(nameEntered,
Integer.parseInt(numberEntered), pickupEntered, destinationEntered,
dateEntered);
 
M

Michael Borgwardt

B said:
I'm trying to pass a date to a method using the simpledateformat parser but
it wont work.

"cannot resolve symbol - constructor Date(java.util.Date)"

Well, that very clearly tells you what the problem is. There is no constructor
of Date that takes a Data as parameter. And why do you try to do that anyway?
DateFormat.parse() returns a perfectly fine Date object , which you can use directly.
 
A

Adam

"cannot resolve symbol - constructor Date(java.util.Date)"
I'm doing it as follows.

Here's the method which expects date to be passed to it...
PickUpDate = new Date(date);

Are you sure you want to make a copy of the 'date'?
AFAIK Date objects are immutable so you
probably can just remember the reference:
PickUpDate = date;
but if i'm wrong about the immutability you can do:
PickUpDate = new Date(date.getTime());

Adam
 
J

Joona I Palaste

B J H said:
The problem being this is due in tomorrow at 11am and I've not even started
implementing any time features. Our lecturer has not told us anything about
Date/Calender classes or how to use them.

11 AM in which time zone? For all I know, it may already be too late to
reply.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Remember: There are only three kinds of people - those who can count and those
who can't."
- Vampyra
 
B

B J H

OK If I try to create the SimpleDateFormat, and then pass the date directly
using dateformat.parse(timedate) i get this error

parse exception; must be caught or declared to be thrown

Here's the code

//create date object using SimpleDateFormat and pass over the date/time
pattern string
SimpleDateFormat dateformat = new
SimpleDateFormat("ddMMyyyyHHmm");

//dateEntered = new Date(dateformat.parse(datetime));

//create new booking passing over the required parameters
Booking t = new Booking(nameEntered,
Integer.parseInt(numberEntered), pickupEntered,
destinationEntered, dateformat.parse(datetime));
 
M

Michael Borgwardt

B said:
OK If I try to create the SimpleDateFormat, and then pass the date directly
using dateformat.parse(timedate) i get this error

parse exception; must be caught or declared to be thrown

http://mindprod.com/jgloss/errormessages.html

You obviously have no experience whatsoever in Java programming and haven't
followed the class material. Frankly, you don't deserve to pass.
 
R

Roedy Green

You obviously have no experience whatsoever in Java programming and haven't
followed the class material. Frankly, you don't deserve to pass.

Saying that of course is just the thing to encourage him to knuckle
down and figure this stuff out. His crime is not understanding
exceptions. You have been overly harsh overgeneralising from that one
observation.

see http://mindprod.com/jgloss/exception.html
 
M

Michael Borgwardt

Roedy said:
Saying that of course is just the thing to encourage him to knuckle
down and figure this stuff out. His crime is not understanding
exceptions. You have been overly harsh overgeneralising from that one
observation.

He seems to lack not only understanding of certain things but also
any kind of experience actually writing even the most trivial
java programs, yet he needs an assignment completed within a narrow
time frame. Reeks to me of somebody slacking off and not visiting
classes and then when he realizes he'll fail he comes running to
the NG asking for help. Admittedly he at least doesn't expect us to
write it for him.
 
B

Bryce (Work)

parse exception; must be caught or declared to be thrown

This error tells you exactly what you should do... Catch the exception
or declare the method with a throws.

Check your text book for the chapter on Exceptions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top