unablissues wit type Date
I started a Java course just a few days ago and I'm not getting far since I've got a bit of a trouble wit hthe type Date.
My current task is to do this:
Add a Patient class to your project which stores the following information about a patient: the patient's reference number, the patient's name, the GP's (family doctor) name and the patient's date of birth as a Date. In addition, the class also stores the patient's arrival time and the time they started their treatment, both of type Time.
Write a constructor which sets the patient's reference number, name, GP's name and date of birth from suitable formal parameters. The date of birth should be represented by three formal parameters of type int which are then passed on as actual parameters to a Date constructor. The time when the patient arrived and the time when they start their treatment should be set to null.
Now, the problem is that I can the Patient class to assign all the needed values inside itself, but I can't seem to be able to make it pass it onto Date. Also, I'm doing this in BlueJ since I'm not ready for a real IDE yet.
At the moment I am only concerned with geting the type Date to work, and perhaps one that is done I'll be able to figre the rest myself. I'm pretty sure that the solution is really simple but I'm very new to this and I'm really confused now.
Can anyone give me a hin or point me in the right direction, or perhaps tell me what I'm doing wrong? Any help at all is greatle appreciated.
(Also it was said that I should have to make any changes to the Date class, which is one of the reasons why I am so confused).
public class Patient
private int patientRefNumber;
private String patientName;
private String gpName;
public static int d;
public static int m;
public static int y;
public static int arrivalTime;
public static int treatmentTime;
* Creates object Patient.
* @param refNumber - sets patient's reference number.
* @param pName - sets patient's name.
* @param gName - sets GP's name.
public Patient(int refNumber, String pName, String gName, int dayOfBirth,
int monthOfBirth, int yearOfBirth)
patientRefNumber = refNumber;
patientName = pName;
gpName = gName;
d = dayOfBirth;
m = monthOfBirth;
y = yearOfBirth;
* Stores date of birth of the patient into Date.
public static void passToDate(int d, int m, int y)
Date day = new Date (d, m, y);
public class Date
/** Fields of a Date - just the day, month and year*/
private int day;
private int month;
private int year;
* Constructor for objects of class Date
* @param d - the day part of the date (1 - 31, depending on the month).
* @param m - the month part of the date (1 - 12).
* @param y - the year part of the date.
public Date(int d, int m, int y)
day = d;
month = m;
year = y;
* @return the date as a String, format "09/11/2002"
public String getAsString ()
return as2Digits(day) + "/" + as2Digits(month) + "/" + year;
/** Internal method to add a leading zero if necessary. */
private String as2Digits (int i)
if (i <10)
return "0" + i;
return "" + i;
Re: unablissues wit type Date
After doing some research and trying out lots of different things i am still unable to get it work as I wanted it to, so I'm skipping this particular task and assigning all the values within Date and Time as opposed to doing it throuth parameters in Patients and then passing them onto Date and Time. At least this way I'll still get some marks for executing the methods properly etc. Its kinda to late to change it anyway since the whole thing is due in in a couple of hours.
If anyone could answer to my previous post I'd appreciate it, even though it won't help me now, it will certainly be useful for me to know it for the future. No rush :)
Re: unablissues wit type Date
Java's date handling is a bit of a mess at the moment - there are much better date libraries, such as Joda Time, which may eventually be incorporated into the SDK in some form.
But with the current SDK, to create a Date object from numeric days, months, and years, you should use a Calendar object (e.g. GregorianCalendar), which is where all calculations involving dates should be done. For example:
To parse a date String into a Date object, you can use a date formatter. SimpleDateFormat is the one to use. You pass it a pattern String that represents the format of the date string, and it uses this to create Date objects from Strings passed to it. For example:
int year = 2009;
int month = 11; // note: Calendar months are 0-based, so 11 is December
int day = 20;
Calendar calendar = new GregorianCalendar(year, month, day);
Date date = calendar.getTime();
String dateStr = "20 Dec 2009";
DateFormat dateformat = new SimpleDateFormat("dd MMM yyyy");
Date date = dateFormat.parse(dateStr);