我正在编写Android应用程序,其中我使用以下代码在当月启用朋友列表:
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";
但现在我想在接下来的30天内获取Facebook好友列表那些生日,我知道使用当前日期获取未来30天的java代码,如下所示:
Date today = new Date();
Calendar cal = new GregorianCalendar();
cal.setTime(today);
cal.add(Calendar.DAY_OF_MONTH, +30);
Date today30 = cal.getTime();
我知道Python查询会在接下来的30天内获取生日,但我不知道我需要在Java中使用哪些代码,如果可能的话请为我替换以下用Java编写的查询: -
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");
所以这里我只想将python代码替换为java代码
答案 0 :(得分:8)
嗯,首先,确保要求friends_birthday
许可,没有它你就不能得到你朋友的生日日期。
如果您不熟悉Graph API Explorer,则应该 - 您可以测试FQL查询以查找问题。
点击“获取访问令牌”,然后在“朋友数据权限”中查看“friends_birthday” 并且你很高兴去测试查询。
其次,所有生日都与实际出生年份一起存储,因此在FQL查询中询问above today
将始终不返回任何内容。
“修复”日期 - 您实际上可以选择生日和今年的一部分,如下所示:
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'
问题出现了:
所选字段不是用户表的一部分,您无法按其排序,甚至不能使用WHERE。 我的猜测是你必须让所有朋友过生日并使用JAVA来过滤数据 - 这应该很容易,因为自定义选定字段中的日期是2013而不是实际年份。
使用此链接随意Test FQL。
编辑#1
我希望明确某些内容,您希望转换的原始查询不会
在接下来的30天内获取Facebook朋友的那些生日列表
在查询中手动替换值时,我看到它返回所有在同一个月内<2>天的生日的朋友。 它没有找到所有在接下来的30天内过生日的朋友。
编辑#2
我已找到一种方法让所有朋友在这个月和下一个月都能过生日 - 这样可以更好地浏览所有朋友列表。
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')
这应该变得更容易查看列表并在今天之前过滤掉这些内容并且从今天起不超过30天 - 这是我能想到的最佳解决方案。
编辑#3 我实际上可以使用编辑#2中的相同查询进行过滤,以获得今天之后的所有查询,由它排序:
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
如果你想限制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
编辑#4
如果您在Java中计算日期并将其传递给查询
,则会更加简单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
编辑#5
一些Java代码:
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";
我没有测试过,没有安装Java: - )