For an assignment for school I need to be able to “calculate” certain dates.
I have a number of contact moments that I need to auto assign to a certain date (day in the week) and print …
Let’s say from the first of January I need to assign every Monday to a contact moment except if this Monday is a holiday or so. I have a list of valid days in my database in date format to check against.
What I have until now :
For the day I was thinking to start at a certain day and then add 7 day’s
I would set the date :
set(int year, int month, int date)
And then adding with 7 to go from week to week :
cal.add(Calendar.DAY_OF_MONTH, 7);
I would then check if this date equals a date in my mysql table.
compareDatesByCalendar(Date javaDate, Date mysqlDate) {
Calendar javaCal = Calendar.getInstance();
Calendar mysqlCal = Calendar.getInstance();
javaCal.setTime(javaDate);
mysqlCal.setTime(mysqlDate);
if (javaCal.equals(mysqlCal)) {
……..
}
else{
….}
If the day is not in the list I would just add 7 more and check again … .
So on until I have all the moments, adding on them to an array to print
Is this the right way to take this on or a better way?
Let’s say we want to make an appointment each Thursday, is there a way to see what is the first day from the starting point (for example starting from first January).?
What if there is a maximum of weeks (moments available)? How could I block this?
Any suggestions would be more then helpfull! Thx all
Since you will be focusing heavily on dates I would recommend you take a look at the Joda API. It offers more functionality than the standard DateTime Java API. You could take a look here to get started.