Apple Grinder
Apple Grinder

Reputation: 3583

Joda Time - Period within a given Period of time?

My use case - I am a doctor. On a given day, I am available for some hours and UN-available for some. I want to create an object Period myWorkDay. When someone fixes an appointment for 8am-9am (ie a Period patient1Appointment), that period is "removed" from myWorkDay. When a new patient accesses myWorkDay, he sees only myWorkDay - patient1Appointment. If patient1 releases his time slot, then the new patient sees full myWorkDay.

Is it possible to do this using JodaTime ?

There is an extra requirement which is NOT necessary. But, if you know how it can be done, then please tell me.

Extra - Define tasks for a particular Period in myWorkDay and execute them. eg. 8-9 Period (8-8:30 Drill teeth, 8:30 - 8:45 polish teeth, 8:45-9:00 do some paperwork and then welcome another patient at 9)

import org.joda.time.*;

public class PeriodManager {


    public static void main(String[]args){

        DateTime startTime = new DateTime(2013, 7, 1, 9, 0);//(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour) 
        DateTime endTime = new DateTime(2013, 7, 1, 17, 0);// 9 to 5 (or 17) job :)

        Period fullDay = new Period(startTime, endTime);
        System.out.println("full day - " + fullDay);

        startTime = new DateTime(2013, 7, 1, 9, 0);
        endTime = new DateTime(2013, 7, 1, 10, 0);
        Period patient1Appointment = new Period(startTime, endTime);//9-10

        fullDay.minus(patient1Appointment);
        System.out.println("full day - pat 1 " + fullDay);

        startTime = new DateTime(2013, 7, 1, 9, 0);
        endTime = new DateTime(2013, 7, 1, 10, 0);
        Period patient1CancelAppointment = new Period(startTime, endTime);

        fullDay.plus(patient1CancelAppointment);
        System.out.println("full day + pat 1 " + fullDay);

        startTime = new DateTime(2013, 7, 1, 9, 0);
        endTime = new DateTime(2013, 7, 1, 10, 0);
        Period patient2Appointment = new Period(startTime, endTime);

        fullDay.minus(patient2Appointment);
        System.out.println("full day - pat 2 " + fullDay);

    }   

}

Upvotes: 2

Views: 873

Answers (1)

Romski
Romski

Reputation: 1942

JODA represents dates and times, so in answer to your question, yes it would be possible to model this at some level using JODA. But you will need to model the problem domain with more than just JODA.

You are essentially doing something like an Outlook calendar. Calendar is already used in Java, so you could call it a diary. A simple diary would know which days are your workdays, and what your work hours are (start time, end time).

You may wish to represent your diary as a day/week/month block. For each day, you can now determine if it is a working day, and what the working hours are.

You will then want to represent an appointment. An appointment at it's simplest has a date, start time and an end time, attendees. You may add other things like description, status (busy, etc.).

When someone creates a new appointment you can check whether it start/end time clashes with an existing appointment.

So starting with this domain driven design we can say that you probably have the following data/entities.

  1. Doctor (probably Employee)
  2. Doctor owns 1 or more Diary
  3. Diary has workdays, work hours; owns Appointment
  4. Appointment has start time,end time, date, Attendee(s)

This will give you a clue to your data and object model, together with master data relationships. There is no reason why you couldn't implement this with the standard classes or JODA.

Upvotes: 1

Related Questions