# DateTime Calculate in apex class [closed]

how to display the following values from the two given date.

"IN"

``````  If does not contatain the Sunday or saturday.
``````

"OUT"

``````   If Sunday or monday appear in the two give date.
``````

Example:

indate : 20 - 9 -2014

Outdate: 21 - 9 -2014

Expected output: Out (because the give date is contain the sunday)

• I'm a bit cofused by this. If it was a Tuesday -> Saturday, would that be IN or OUT? Sep 24, 2014 at 10:38
• It should show both. I like this question. Sep 24, 2014 at 11:28
• Ok cool, I was a bit confused by the overlaps! Sep 24, 2014 at 11:34

You could build on the logic below to implement what you are looking for

``````public Boolean isSunday(Date d) {
Integer dayOfWeek = Math.mod(Date.newInstance(1960, 1, 3).daysBetween(d), 7);
// 0 = Sunday, 1 = Monday, 2 = Tuesday...
return dayOfWeek == 0;
}
``````

The code uses a known Sunday to figure out the number of days between that date and the date you are comparing. It then does the `Math.mod` to find out the weekly offset from Sunday.

Another option would be to format the Date into the actual day, as shown below:

``````public Boolean isSunday(Date d) {
String dayOfWeek = ((DateTime) d).format('EEEE');
return dayOfWeek == 'Sunday';
}
``````
• is this applicable if we give the this monday to next monday? Sep 24, 2014 at 11:29
• Hi @Lemon, I don't understand your question. Sep 24, 2014 at 11:32
• Is this work, if the Sunday contain in-between that two date? Sep 24, 2014 at 16:47
• This is not the solution to your problem, it is an example of how you can check a date to see if it's a particular day, so that you can build on this logic to implement your solution. Sep 24, 2014 at 17:05
• How to use the loop for iterate the date? Sep 25, 2014 at 3:12

So we can use this code (as provided by Daniel Blackhall here) to work out the day of the week (indexed from 0 for a monday to 6 for a Sunday):

``````Date monday = Date.newInstance(1900, 1, 1);
Integer firstDay = Math.mod(monday.daysBetween(date1), 7);
``````

Which is quite useful when we want to check these rules out (assuming date1 comes before date2, swap if not!).

``````Integer first = Math.mod(monday.daysBetween(date1), 7);
Integer second = Math.mod(monday.daysBetween(date2), 7);

// Default to start
String output = 'IN';

// easy case, if there's six days or more between the dates, then it includes a Saturday and/or Sunday
if(date1.daysBetween(date2) >= 6)
{
output = '';
}
else
{
// less than a six days, so let's check the range
if(first >=5 || second >= 5 || second < first)
{
output = '';
}
}
``````

So, right now, output will be "IN" if the range doesn't contain Saturday or Sunday. Now we can do the other part... again if there's more than 6 days then a Sunday or Monday must be included. Ff there isn't then it's a case of checking the day values if the first date is earlier in the week. If the dates wrap around then again, a Sunday or a Monday must be included.

``````if(date1.daysBetween(date2) >= 6)
{
output += ' OUT';
}
else if(second > first && (first == 0 || second == 6)
{
output += ' OUT';
}
else if(second < first)
{
output += ' OUT';
}
``````

Of coruse this could be condensed a little but I've left each test distinct to make it easier to read.

• you have used the date.. but my value is datetime.. so please assign the monday value is datetime and let em know how can achieve? Sep 24, 2014 at 17:28
• It's basically identical... should be able to work it out from this! Sep 24, 2014 at 21:54
• Is this ittrate the date for checking every date? Sep 25, 2014 at 3:13
• Nope, because you don't need to check every date! You just need to check certain conditions about the range of dates. Sep 25, 2014 at 3:18
• you are using the date. but my value is date time. because if the cross the 8:30 on friday it should out off office so the output is out. how to implement that? I got issue form above code due to the datetime and date. Sep 25, 2014 at 5:06