-1

I have a query regarding to find out which is day (in given list) is nearest day from today.

For example

day_list is Mon, Tue, Fri, Sat
and today is Wed

Now If we calculate days on paper,

Wed to Mon = 5 days
Wed to Tue = 6 days
Wed to Fri = 2 days
Wed to Sat = 3 days 

So answer will be Fri .

But in code, how can I implement this logic? Or is there any other way to optimize its process (regarding if_else)?

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • I am sure you just want to get no. of days between two dates? – Paresh Mayani Nov 09 '11 at 10:04
  • If this is the case then check this link: http://stackoverflow.com/questions/3838527/android-java-date-difference-in-days/6406294#6406294 – Paresh Mayani Nov 09 '11 at 10:04
  • My question is little bit different, I have only list of some days_of_week and I have to find which is nearest from today. If using the logic of your link, my problem is solvable, please guide me how can I implement that logic. Thank you! – Pankaj Kumar Nov 09 '11 at 10:11
  • Mon = 0, Tue = 1 and so on ... in your example Wed = 2 and list = [0,1,4,5] ... for each day smallest from 2[Wed] add 7 so we have [7,8,4,5] ... for each days sub Wed so we have [5,6,2,3] ... find smallest(fx. sort array and get first) in array result 2 ... add Wed result 4 ... 4 means Fri :) – Selvin Nov 09 '11 at 10:27

2 Answers2

2

What I have in mind is below but probably you should change a little bit to fit your need.

Give your days of week number from 0 to 6.

Sun = 0; Mon = 1; Tue = 2; etc.

If you just have some of days, keep the numbers. For example, if you have only Mon, Tue, Fri, and Sat then keep the number respective to the days, i.e. 1, 2, 5, and 6.

Now, if you want to know the "distance" between those days from Wed (which has a number 3), you can do this math:

distance = (theNumberOfYourDayInList - 3) mod 7

7 is the number of the days, by the way.

So if you want to know the distance between:

Wed to Mon: distance = (1 - 3) mod 7 which is 5.

Wed to Fri: distance = (4 - 3) mod 7 which is 1.

Wed to Tue: distance = (2 - 3) mod 7 which is 6.

Turn those logic into codes are trivial and I think you shouldn't face any problems.

Kristiono Setyadi
  • 5,635
  • 1
  • 19
  • 29
  • It could be very good implementation. In current I am implementing logic of selvin. And sure I will implement your logic too. +1 for simplest logic. Thank you! – Pankaj Kumar Nov 09 '11 at 11:34
1

Just count them

int Count= days(Calendar.WEDNESDAY, Calendar.TUESDAY);

private int days(int Day1, int Day2) {
    int Days = 1;
    while (Day1 != Day2) {
        Day1 += 1;
        Days += 1;
        if (Day1 == Calendar.SATURDAY) Day1 = Calendar.SUNDAY;
    }
    return Days;
}
Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • So check for that or initialise Days to 0. This was an example. I'm not writing your app for you. This was modified from a function where I needed the count in the NEXT week. i.e Tues-Wed would be 8 days – Kuffs Nov 09 '11 at 12:36
  • I know you wont write my application. I thought it could be a good logic, but I realised that it will take some time to find its bug. Anyway thank you! – Pankaj Kumar Nov 09 '11 at 12:44