How to check array of dates are consecutive from todays date? [duplicate] - java

This question already has answers here:
Java - Check if array contains 3 consecutive dates
(4 answers)
Closed 5 years ago.
I have an array of unique dates from each time the user completes a task. I want to check if the dates within the array are consecutive from and including todays date.
If the array contains dates: "2017/6/2, 2017/6/3, 2017/6/4, 2017/6/5" then based on today's date being 2017/6/5 the function would return 4 as there are 4 consecutive dates from and including today.
If the array contains dates "2017/6/2, 2017/6/3, 2017/6/4" then it would return 0 as the array does not include today's date. Otherwise the count would be broken upon a non consecutive date.
List<Date> dateList = new ArrayList<Date>();
int count = 0;
Date todayDate = new Date();
for (int i=0; i<dateList.size(); i++){
// Check if dates within the array are consecutive from todayDate, if so then increment count by 1.
}

If you're using Java 8, consider using the new java.time API. It's easier, less bugged and less error-prone than the old APIs.
If you're using Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, there's the ThreeTenABP (more on how to use it here).
Although you can also use JodaTime, it's being discontinued and replaced by the new APIs, do I don't recommend start a new project with joda. Even in joda's website it says: "Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).".
As you want to compare just the date (day/month/year), and not the time (hour/minute/second), the best choice is to use the LocalDate class. For java 8, this class is in java.time package, and in ThreeTen Backport, the package is org.threeten.bp. But the classes and methods names are the same.
The code would be like this:
public int count(List<LocalDate> dateList, LocalDate today) {
if (!dateList.contains(today)) { // today is not in the list, return 0
return 0;
}
int count = 0;
LocalDate prev = dateList.get(0); // get first date from list
for (int i = 1; i < dateList.size(); i++) {
LocalDate next = dateList.get(i);
if (prev.plusDays(1).equals(next)) {
// difference between dates is one day
count++;
} else {
// difference between dates is not 1
// Do what? return 0? throw exception?
}
prev = next;
}
return count + 1; // didn't count the first element, adding 1
}
Testing this method:
List<LocalDate> dateList = new ArrayList<>();
dateList.add(LocalDate.of(2017, 6, 2));
dateList.add(LocalDate.of(2017, 6, 3));
dateList.add(LocalDate.of(2017, 6, 4));
dateList.add(LocalDate.of(2017, 6, 5));
LocalDate today = LocalDate.now();
System.out.println(count(dateList, today)); // 4
Another test (when today is not in the list)
List<LocalDate> dateList = new ArrayList<>();
dateList.add(LocalDate.of(2017, 6, 2));
dateList.add(LocalDate.of(2017, 6, 3));
dateList.add(LocalDate.of(2017, 6, 4));
LocalDate today = LocalDate.now();
System.out.println(count(dateList, today)); // 0
Notes:
As it wasn't specified what to do when the days are not consecutive (return 0 or throw exception), I left this part commented. But it should be straightforward to add this to the code
If you want to convert java.util.Date to LocalDate, you can do as follows (using the code of this answer, full explanation is in this link in case you have any questions):
public LocalDate convert(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
// if your Date has no toInstant method, try this:
public LocalDate convert(Date date) {
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
}
I understood that you want to check for consecutive days (so, a 1-day difference between the dates). But if you want to check if the previous date is before the next (no matter how many days), you can change the if (prev.plusDays(1).equals(next)) to if (prev.isBefore(next))
I'm not sure if that's the case, but if you want, you can also parse a String directly to a LocalDate (so you don't need to create lots of Date objects), using a DateTimeFormatter:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d");
LocalDate d = LocalDate.parse("2017/6/2", formatter); // 2017-06-02

There are a lot of ways to write it more clear:
Use new Date API;
Use libraries;
But, in such case, with usage of old Date classes, I would do that in such a way:
public static void main(String[] args) {
long millisInDay = TimeUnit.DAYS.toMillis(1);
List<Date> dates = Arrays.asList(new Date("2017/6/2"), new Date("2017/6/3"), new Date("2017/6/4"), new Date("2017/6/5"));
System.out.println(getSequentialNumber(millisInDay, dates));
}
private static int getSequentialNumber(long millisInDay, List<Date> dates) {
int count = 0;
Date now = setMidnight(Calendar.getInstance().getTime());
for (int i = dates.size() - 1; i >= 0; i--) {
Date date = setMidnight(dates.get(i));
if (date.getTime() == now.getTime()) {
count++;
}
now.setTime(now.getTime() - millisInDay);
}
return count;
}
private static Date setMidnight(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
return calendar.getTime();
}

If I understand the requirement correctly, you have an array of Date objects, ordered by date, and guaranteed not to have two Date objects for the same day, but possibly with gaps between the days. Your goal is to return the length of the maximum sub-array that contains only consecutive days and also includes the current day, or to return 0 if there is no such sub-array. The current day may fall anywhere inside that sub-array, not necessarily at the beginning or end.
It's not clear if you need to support crossing year boundaries, but I'll assume so. I also assume that all the Date objects in the list are for the same time zone which is also the time zone for the device on which you are running. If that's not the case, you should refer to this answer for more information on testing whether two Date objects refer to the same day.
It's fairly simple to do this if you work with Calendar objects instead of Date objects. You don't need any third-party libraries, as both Date and Calendar are parts of the standard Android API. I suggest doing this in two phases: first search for the current date in the array and then scan in both directions for either a gap in the dates or an array boundary. Then just count how far you could go in each direction.
public int getDateSpanCount(List<Date> dateList) {
final int n = dateList.size();
final Calendar today = Calendar.getInstance();
final Calendar other = Calendar.getInstance();
int count = 0;
// First search for today in the date array
int posToday = -1;
for (int i=0; i<n; i++) {
other.setTime(dateList.get(i));
if (areSameDay(today, other)) {
posToday = i;
break;
}
}
// If today is in the list, count the size of the sub-array containing today
if (posToday >= 0) {
count++; // count today, at least
final Calendar probe = Calendar.getInstance();
// scan backwards from position of today's date
for (int prevPos = posToday - 1; prevPos >= 0; prevPos--) {
final Date prev = dateList.get(prevPos);
probe.setTime(prev);
other.add(Calendar.DAY_OF_YEAR, -1);
if (areSameDay(probe, other)) {
count++;
other.setTime(prev);
} else {
break;
}
}
// reset the other time
other.setTime(today.getTime());
// scan forward from position of today's date
for (int nextPos = posToday + 1; nextPos < n; nextPos++) {
final Date next = dateList.get(nextPos);
probe.setTime(next);
other.add(Calendar.DAY_OF_YEAR, 1);
if (areSameDay(probe, other)) {
count++;
other.setTime(next);
} else {
break;
}
}
}
return count;
}
/** Test whether two Calendar objects are set to the same day */
private static boolean areSameDay(Calendar c1, Calendar c2) {
// see discussion above if dates may not all be for the local time zone
return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) &&
c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR);
}

Related

How to RESET Arraylist at the end of the week?

I have an arraylist:
ArrayList<String> wholeWeekArrayList = new ArrayList<>();
DatabaseHelper(SQLite) is storing the values into the(above) arrayList:
Cursor data = mDatabaseHelper.getData();
while (data.moveToNext() ) {
wholeWeekArrayListString.add(data.getString(1));
//getString(1) is column values from SQLite
}
Then I have SumofWeek where all the data in wholeWeekArrayListString is added together.
(I Convert wholeWeekArrayListString to double to view it in a TEXTVIEW);
I want this SumOfWeek to reset to Zero at the end of the week but the data inside the SQLite must keep adding(for SumOfMonth view).
So by Sunday the data may be $50.00 (for Example) and Starting on int Monday. the data must reset to 0.0 and sum all the way up until Sunday.
This must happen weekly.
How would u do this? I have tried assining days of weeks.
Monday =1;
Tuesday = 2;
Wednesday =3;
// ...
and iterate through the whole week but Im not able to get(i), which is the data of each single day of the week and then reset it on which is
int Sunday =7;
I have the total amount but dont know what method to use to reset the data at the end of the week?
Without changing the DB calls, you´ll have to store the timestamp of the latest reset (resetTimestamp in the example below). If the current time is in a different week, the code will trigger a reset of sumofWeek. After that, it´s your normal sumofWeek logic.
Thread safety: to be added depending on the application specifics.
private ArrayList<Long> sumofWeek = new ArrayList<>();
private Long resetTimestamp;
public void populateWeek() {
if(resetTimestamp == null || isDifferentWeek(resetTimestamp)) {
sumofWeek = new ArrayList<Long>();
for(int i = 0; i < 7; i++){
sumofWeek.add(0l);
}
System.out.println(sumofWeek);
resetTimestamp = java.lang.System.currentTimeMillis();
}
// populate sumofWeek here
}
private boolean isDifferentWeek(long resetTimestamp) {
Calendar cl1 = new GregorianCalendar();
cl1.setTimeInMillis(java.lang.System.currentTimeMillis());
Calendar cl2 = new GregorianCalendar();
cl2.setTimeInMillis(resetTimestamp);
return cl1.get(Calendar.WEEK_OF_YEAR) != cl2.get(Calendar.WEEK_OF_YEAR);
}

Given a day and time value, how can I find the next class in my timetable?

I have an array of cell[] containing all classes for a student ID, each cell has two variables relevant to this context:
int day and int startTime.
I also have two given values for the currentDay and currentTime, both in the same format as cell's day and startTime
I want to make a loop that finds the cell element that contains the next class.
I've tried looping through the cell array and selecting the closest day that contains at least one class, with some success, and I imagine that I would need to make another array that contains all the classes for that day then do the same logic to them. I just can't figure out how.
public Cell getNextClass(int studentID, Context context){
DBHelper dbHelper = new DBHelper(context);
Cell[] cell = dbHelper.queryCellData(studentID);
Date currentTime = Calendar.getInstance().getTime();
int currentDay = getCurrentDay(currentTime.toString());
Log.d(TAG, "currentDay: " + currentDay);
DateFormat dateFormat = new SimpleDateFormat("hh a");
String formattedDate= dateFormat.format(currentTime);
int currentHour = getCurrentHour(formattedDate);
//TODO Find next class given currentDay and currentHour
Cell nextClass = cell[0];
for (int i = 0; i < cell.length; i++) {
if (cell[i].getDay() >= currentDay && cell[i].getDay() <= nextClass.getDay()){
//?????
}
}
return nextClass;
}
}
In this example, cell[0] has one class on hour 12, and is displayed by default because the loop doesn't change anything. However at time of posting in NZ the values would be:
currentDay = 2
currentHour = 21
As shown in this screenshot of my timetable: i.imgur.com/X6HzR6y.png
The next class will be tomorrow, day 3, hour 8.
I recommend that you use java.time classes for the fields of your Cell class, not int:
public class Cell {
DayOfWeek day;
LocalTime startTime;
// Constructor, getters, toString, etc.
}
Also if you can, fit your class with a method that given a day of week and time calculates how long it is until the class occurs next time:
public Duration getTimeUntilNext(DayOfWeek currentDay, LocalTime currentTime) {
int daysUntil = day.getValue() - currentDay.getValue();
Duration time = Duration.ofDays(daysUntil).plus(Duration.between(currentTime, startTime));
if (time.isNegative()) { // over for this week
// Calculate time until next week’s occurrence
time = time.plusDays(7);
assert ! time.isNegative() : time;
}
return time;
}
If you cannot add this method to the Cell class, you may declare it a static method somewhere else and just add the cell as an extra parameter.
Now the rest is not so bad:
Cell[] classes = new Cell[] {
new Cell(DayOfWeek.THURSDAY, LocalTime.of(12, 0)),
new Cell(DayOfWeek.MONDAY, LocalTime.of(14, 0)),
new Cell(DayOfWeek.THURSDAY, LocalTime.of(10, 0)),
new Cell(DayOfWeek.FRIDAY, LocalTime.of(9, 0)),
new Cell(DayOfWeek.THURSDAY, LocalTime.of(6, 0))
};
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Pacific/Auckland"));
final DayOfWeek currentDay = now.getDayOfWeek();
final LocalTime currentTime = now.toLocalTime();
Comparator<Cell> comparatorByTimeUntilNext = new Comparator<Cell>() {
#Override
public int compare(Cell c1, Cell c2) {
return c1.getTimeUntilNext(currentDay, currentTime)
.compareTo(c2.getTimeUntilNext(currentDay, currentTime));
}
};
Cell nextClass = Collections.min(Arrays.asList(classes), comparatorByTimeUntilNext);
System.out.println("Next class from " + now + " is " + nextClass);
When I ran this just now, it output:
Next class from 2019-06-20T06:49:23.188+12:00[Pacific/Auckland] is THURSDAY at 10:00
Even if you stick with int fields in your class, you should still be able to use the idea. Then getTimeUntilNext may either still return a Duration or an int denoting the number of hours. I recommend the former, of course.
It seems to me that your code was formatting the current date and time into two different strings and parsing each of them back to get the day and time. That’s certainly the detour. Also as I already said in a comment, I recommend you avoid Date, Calendar, DateFormat and SimpleDateFormat since they are all poorly designed and long outdated.
Question: Can I use java.time on Android?
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Here is a good entry point for you: https://docs.oracle.com/javase/tutorial/datetime/iso/index.html
I do understand that you don't want to change your code. But since your application deals with dates and time it should use the build in api's the java language gives you. Just try to introduce it a controlled and testable way. The String class is also massive, but I bet you use that one, and not your own implementation.
Thank you for your reference, #Jocke however, I found my own way of doing it (poorly), so for posterity and the sake of any poor student who coded themselves into the same situation as me, I just added a bunch of messy edge cases for how time works in looped week scenarios like timetables. I will be using default libraries when possible in the future. Don't you worry. Here's the final method that I used.
public Cell getNextClass(int studentID, Context context){
DBHelper dbHelper = new DBHelper(context);
Cell[] cell = dbHelper.queryCellData(studentID);
Date currentTime = Calendar.getInstance().getTime();
int currentDay = getCurrentDay(currentTime.toString());
Log.d(TAG, "currentDay: " + currentDay);
DateFormat dateFormat = new SimpleDateFormat("hh a");
String formattedDate= dateFormat.format(currentTime);
int currentHour = getCurrentHour(formattedDate);
ArrayList<Cell> classesInDay = new ArrayList<>();
if (currentHour >= 17){
currentDay++; //If the current hour is past the end of the day, add 1 to the day so that tomorrow is scanned.
}
if (currentDay > 4){
currentDay = 0; //If the current day is greater than 4 (the weekend) set day to monday.
}
for (int i = 0; i < cell.length; i++) {
if (cell[i].getDay() == currentDay){
Log.d(TAG, "getNextClass: cell " + i +" has a class today");
classesInDay.add(cell[i]);
}
}
Cell nextClass = classesInDay.get(0);
Log.d(TAG, "getNextClass: "+classesInDay.size());
//If today's classes are over, then search tomorrow.
if (classesInDay.size() == 1 && classesInDay.get(0).getStartTime() < currentHour){
classesInDay.clear();
currentDay++;
for (int i = 0; i < cell.length; i++) {
if (currentDay > 4){
currentDay = 0; //If the current day is greater than 4 (the weekend) set day to monday.
}
if (cell[i].getDay() == currentDay){
Log.d(TAG, "getNextClass: cell " + i +" has a class today");
classesInDay.add(cell[i]);
}
}
nextClass = classesInDay.get(0); //ReApply default class for new day
}
for (int i = 1; i < (classesInDay.size()) ; i++) {
int diff1 = classesInDay.get(i).getStartTime() - currentHour-1; //Minus one to ensure that the next class if found, not the current one.
int diff2 = nextClass.getStartTime() - currentHour-1;
Log.d(TAG, "diff1: "+diff1+" diff2: "+diff2);
if (diff1 < 0){ //This means that the Test cell is before the current hour
}else if(diff2 < 0){ //This means that the current choice is before the current hour
nextClass = classesInDay.get(i);
}else if (diff1 < diff2){ //This means that the test cell is closer to the current hour than the current choice
nextClass = classesInDay.get(i);
}else if (diff2 < diff1){ //This means that the current choice is closer to the current hour than the test cell
}
}
return nextClass;
}

Java : given a list of object that has range of dates find two objects whose end month is closest to current date month

List of dates are as below (The list can be in any order):
3-Jan to 31-Mar, 2-Apr to 30-Jun, 1-Jul to 30-Sep, 4-Oct to 31-Dec
Current Date is: 19-Feb
Can someone please help me with the logic?
My approach is:
if(the given date should be greater than start date and less than end date){//this gives current quarter}else if(difference of the month of current date from the end date of each object should be less than or equal to 5)
i am hard coding the condition less than 5, which may break if in future the range of date will be of 4 months
Second approach is:
we can sort the list in ascending order and can get the current quarter index by comparing with current date and the next quarter will be of next index. But the complexity will be more.
I tried below code, but it gives only current quarter date. I am not able to get next quarter considering there would be only 3 objects and current date month is feb.
public static List getCurrentQtrOffr(List detail,Date currentDate) throws ParseException{
int currentQuarter = 9999, diff1;
int nextquarter = 9999, diff2;
Detail detail1;
Detail detail2;
Detail detail3 = null;
Detail detail4 = null;
Iterator<Detail> iterator = detail.iterator();
List<Detail> list = new ArrayList<Detail>();
while(iterator.hasNext()){
detail1 = iterator.next();
diff1 = getDiff(currentDate,detail1.startTime());
if(diff1>0){
if(iterator.hasNext()){
detail2 = iterator.next();
}else{
detail2 = null;
}
if(detail2 != null){
diff2 = getDiff(currentDate,detail2.startTime());
if(diff1 < diff2 ){
if(currentQuarter > diff1){
nextquarter = currentQuarter;
currentQuarter = diff1;
//how to assign detail3 before updating it with next minimum value, as if there will be only 3 object and flow comes in this if block then detail4 will be null
detail4=detail3;
detail3=detail1;
}else if(nextquarter > diff1){
nextquarter = diff1;
detail4=detail1;
}
}else{
if(currentQuarter > diff2){
nextquarter = currentQuarter;
currentQuarter = diff2;
detail4=detail3;
detail3=detail1;
}else if(nextquarter > diff2){
nextquarter = diff2;
detail4=detail1;
}
}
}else{
if(currentQuarter > diff1){
nextquarter = currentQuarter;
currentQuarter = diff1;
detail4=detail3;
detail3=detail1;
}else if(nextquarter > diff1){
nextquarter = diff1;
detail4=detail1;
}
}
}else{
System.out.println("skipped "+diff1);
}
}
list.add(detail3);
list.add(detail4);
return list;
}
If the periods are mutually exclusive (not overlapping) the you simply check for the first occurrence where:
The target is equal to or later than the start, and…
The target is before the stop.
This logic follows the Half-Open approach commonly used in date-time work where the beginning is inclusive while the ending is exclusive.
A shorter way of saying "the target is equal to or later than the start" is "not before start". The exclamation mark ! means not in Java syntax.
Boolean periodContainsTarget = ( ! target.isBefore( start ) ) && target.isBefore( stop ) ;
The above logic would be used with LocalDate if you meant date with a year. If you literally meant a month and day without a year, use the MonthDay class. The logic works for both.
Use Period class to represent the span of time between a pair of LocalDate objects. See Tutorial.
You might also find useful the Interval class in the ThreeTen-Extra project that supplements java.time.

Java Joda-Time , assign LocalDate to Month and Year

I have never used Joda-Time before but I have ArrayList which contains objects with LocalDate and count. So I have count for each day in ArrayList and each day is only once in ArrayList.
I need to calculate counts for each month of year, which is in list.
My data:
E.g.:
dd.MM.yyyy
17.01.1996 (count 2)
18.01.1996 (count 3)
19.02.1996 (count 4)
19.03.1996 (count 1)
18.05.1997 (count 3)
Now I want outpur like this:
MM.yyyy
01.1996 -> 2 (17.1.1996) + 3 (18.1.1996) = 5
02.1996 -> 4 (19.2.1996) = 4
03.1996 -> 1 (19.3.1996) = 1
05.1997 -> 3 (18.5.1997) = 3
Simply I need to get count for each month, but I do not know what would be best way to achieve this.
Data class:
private class Info{
int count;
LocalDate day;
}
And result I would put in some class which contains Month and Year date + count.
In Joda-Time, there is class that represents Year + Month information, named YearMonth.
What you need to do is mostly construct a Map<YearMonth, int> to store the count of each YearMonth, by looping through your original List which contains LocalDate and count, and update the map accordingly.
Conversion from LocalDate to YearMonth should be straight forward: YearMonth yearMonth = new YearMonth(someLocalDate); should work
in pseudo-code, it looks like:
List<Info> dateCounts = ...;
Map<YearMonth, Integer> monthCounts = new TreeMap<>();
for (Info info : dateCounts) {
YearMonth yearMonth = new YearMonth(info.getLocalDate());
if (monthCounts does not contains yearMonth) {
monthCounts.put(yearMonth, info.count);
} else {
oldCount = monthCounts.get(yearMonth);
monthCounts.put(yearMonth, info.count + oldCount);
}
}
// feel free to output content of monthCounts now.
// And, with TreeMap, the content of monthCounts are sorted
You are looking for the getMonthOfYear and getYear methods on the LocalDate class in Joda-Time 2.3.
for ( Info info : infos ) {
int year = info.day.getYear();
int month = info.day.getMonthOfYear();
}
From there, write code to roll-up the count in any way that suits you. You could keep a map of years as keys leading to a map of months. You could create a string in the format of "YYYY-MM" as a key to map.

How to determine if the specific time is between given range?

Problem: I have a list containg hours, for example:
08:15:00
08:45:00
09:00:00
12:00:00
...
application is allowing user to make an appointment for a specific hour let'say: 8:15:00, each meeting takes half an hour.
Question: How to determine if there is a slot needed for appointment like this? I know that Calendar class have methods before() nad after(), but it doesn'solve my problem. I mean if there is appointment at 12:00 and another one at 12:00, how to prevent before making another one at 12:15?
edit:
I've tried using methods I mentioned before, like:
Calendar cal1 = Calendar.getInstance(); // for example 12:00:00
Calendar cal2 = Calendar.getInstance(); // for exmaple 12:30:00
Calendar userTime = Calendar.getInstance(); // time to test: 12:15:00
if(user.after(cal1)&& user.before(cal2)){
... // do sth
}
Check if the date to check is between the two provided:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm");
Date before = sdf.parse("07/05/2012 08:00");
Date after = sdf.parse("07/05/2012 08:30");
Date toCheck = sdf.parse("07/05/2012 08:15");
//is toCheck between the two?
boolean isAvailable = (before.getTime() < toCheck.getTime()) && after.getTime() > toCheck.getTime();
To book for a determinate hour, I would do a class with two dates and a method to check this:
public class Appointment{
private Date start;
private Date end;
public boolean isBetween(Date toCheck){....}
}
Then you can simply do an Schedule class extending ArrayList, adding a method isDateAvailable(Date toCheck), iterating the list of Appointments and checking that there is no one conflicting.
I'd have some kind of appointment class with either a start timestamp and a duration or a start time and an end time. Then when adding new appointments to the schedule, check that the appointment with the start time before the new appointment doesn't run over the start time of the proposed new appointment.
Well how you would do it specifically depends on how you are storing your data, format, etc., but generally what you would do is simply check if there is an appointment for any time between the requested time to the requested time + requested length.
// Example (using int time(1 = 1 minute), assuming that appointments can only be at 15min intervals)
boolean isHalfHourTimeSlotAvaliable(int time) {
for (int i = 0; i < appointments.size(); i++) {
if (appointments.get(i).time == time || appointments.get(i).time == time + 15) {
return false;
}
}
return true;
}

Categories

Resources