I am writing Android Application, in which i am fetching list of friends those birthdays in current month by using below code:
Calendar c = Calendar.getInstance();
int month = c.get(Calendar.MONTH) + 1;
String query = "select name, birthday, uid, pic_square from user
where (substr(birthday_date, 0, 2) =" + month +") AND uid IN
(SELECT uid2 FROM friend WHERE uid1 = me()) order by birthday_date ASC";
but now i want to fetch list of facebook friends those birthdays in next 30 days, i know the java code to get next 30 days by using current date like below:
Date today = new Date();
Calendar cal = new GregorianCalendar();
cal.setTime(today);
cal.add(Calendar.DAY_OF_MONTH, +30);
Date today30 = cal.getTime();
I know Python query to fetch Birthdays coming in next 30 days, but i don't know what code i need to use in Java, if possible so please replace write below query in Java for me:-
query = "SELECT username FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND (substr(birthday_date, 0, 2) = {0} AND substr(birthday_date, 3, 5) >= {1} AND substr(birthday_date, 3, 5) <= {2})").format(today.strftime("%m"), today.strftime("%d"), today30.strftime("%d");
So here i just want to replace python code to java code
Well, first of all, make sure to ask for friends_birthday
permission, without it you can't get your friends birthday dates.
If you are not familiar with the Graph API Explorer, you should - you can test FQL queries to find problems.
Click the "Get Access Token" and in "Friends Data Permissions" check the "friends_birthday"
and you are good to go testing the query.
Second, all the birthdays are stored with the actual birth year, so asking for above today
in FQL query will always return nothing.
to "fix" the date - you can actually select a portion of the birthday and this year to it like so:
SELECT name, birthday, birthday_date, concat(substr(birthday_date,0,6),"2013") FROM user
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())
AND birthday_date != 'null'
And here comes the problem:
The selected field is not part of the user table, and you can't sort by it, or even use WHERE on it.
My guess is that you have to get all your friends birthday and use JAVA to filter the data - It should be easy since the date in the custom selected field has 2013 instead of the real year.
Feel free to Test the FQL with this link.
EDIT #1
I want to be clear on something, the original query you wish to convert will not
fetch list of Facebook friends those birthdays in next 30 days
When replacing the values manually in the query I saw that it returns all friends with birthday between 2 given days in the same month.
It does not find the all the friends who has a birthday in the next 30 days.
EDIT #2
I have found a way to get all friends with birthday in this month and the next - which is much better the to go over all the friends list.
SELECT name, birthday, birthday_date, concat(substr(birthday_date,0,6),"2013") FROM user
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())
AND birthday_date != 'null'
AND (substr(birthday_date,0,2)='02' OR substr(birthday_date,0,2)='03')
This should become much easier to go over the list and filter the ones before today and no more then 30 days from today - this is the best solution I can think of.
EDIT #3
I can actually filter using the same query from edit #2 to get all the ones after today, ordered by it:
SELECT name, birthday, birthday_date, concat(substr(birthday_date,0,6),"2013") FROM user
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())
AND birthday_date != 'null'
AND (substr(birthday_date,0,2)='02' OR substr(birthday_date,0,2)='03')
AND birthday_date > '02/23/2013'
ORDER BY birthday_date
And if you want to limit by 10:
SELECT name, birthday, birthday_date, concat(substr(birthday_date,0,6),"2013") FROM user
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())
AND birthday_date != 'null'
AND (substr(birthday_date,0,2)='02' OR substr(birthday_date,0,2)='03')
AND birthday_date > '02/23/2013'
ORDER BY birthday_date
LIMIT 10
Edit #4
Much more simple if you calculate the dates in Java and pass it to the query
SELECT name, birthday, birthday_date FROM user
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())
AND birthday_date != 'null'
AND birthday_date > '02/23/2013'
AND birthday_date < '04/24/2013'
ORDER BY birthday_date ASC
EDIT #5
Some Java code:
Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String today_formatted = formatter.format(today);
cal.add(Calendar.DATE, 30);
String today_plus30_formatted = formatter.format(cal.getTime());
String query =
"SELECT name, birthday, birthday_date FROM user
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())
AND birthday_date != 'null'
AND birthday_date > '" + today_formatted + "'
AND birthday_date < '" + today_plus30_formatted + "'
ORDER BY birthday_date ASC";
I have not tested it, don't have Java installed :-)