Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: unablissues wit type Date

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default unablissues wit type Date

    Hey guys,

    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;
            }
            else 
            {
                return "" + i;
            }
        }
    }


  2. #2
    Junior Member
    Join Date
    Dec 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default 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:
    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();
    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:
    String dateStr = "20 Dec 2009";
    DateFormat dateformat = new SimpleDateFormat("dd MMM yyyy");
    Date date = dateFormat.parse(dateStr);

Similar Threads

  1. How to set expiry date for cms?
    By khodam in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: December 4th, 2009, 01:15 PM
  2. [SOLVED] how to get phone time and date
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: August 26th, 2009, 11:15 AM
  3. Java error "java.lang.StackOverflowError"
    By sanatkumar in forum Exceptions
    Replies: 2
    Last Post: July 7th, 2009, 02:40 PM
  4. Type casting error in Java
    By Eric in forum Java Theory & Questions
    Replies: 3
    Last Post: December 13th, 2008, 04:11 PM
  5. How to Get the current date and time
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 2nd, 2008, 01:55 PM