如何在Android中定制Facebook好友生日

如何在Android中定制Facebook好友生日,android,facebook,Android,Facebook,我制作了一个应用程序,在该应用程序中,我将获得我所有Facebook好友生日的列表,如下所示: try { DateFormat df1 = new SimpleDateFormat("MM/dd/yyyy"); DateFormat df2 = new SimpleDateFormat("MMM dd, yyyy"); return df2.format(df1.parse(input)); } catch (ParseException e) { return null; }

我制作了一个应用程序,在该应用程序中,我将获得我所有Facebook好友生日的列表,如下所示:

try {
  DateFormat df1 = new SimpleDateFormat("MM/dd/yyyy");
  DateFormat df2 = new SimpleDateFormat("MMM dd, yyyy");
  return df2.format(df1.parse(input));
}
catch (ParseException e) {
  return null;
}
瑞奇

1990年2月13日

但现在我想展示这样的东西:

try {
  DateFormat df1 = new SimpleDateFormat("MM/dd/yyyy");
  DateFormat df2 = new SimpleDateFormat("MMM dd, yyyy");
  return df2.format(df1.parse(input));
}
catch (ParseException e) {
  return null;
}
瑞奇

1990年2月13日

代码:-

MyAdapter.java

 TextView name = (TextView) v.findViewById(R.id.label);
    name.setText(friend.getName());

    TextView bday = (TextView) v.findViewById(R.id.label2);
    if (!friend.getBday().trim().equals("")) {
        bday.setText(friend.getBday());
    } else {
        bday.setText("Not Mentioned");
    }

    return v;
}
  public void init(Main main) {
    this.main = main;
    mFacebook = new Facebook(APP_ID);
    isReady = false;
    mAsyncRunner = new AsyncFacebookRunner(mFacebook);
    mFacebook.authorize(main, new String[] { "publish_stream",
            "friends_birthday" }, new MyAuthorizeListener());
}

// ref- http://developers.facebook.com/docs/reference/api/user/
public Report reLoadAllFriends() {
    if (!isReady) {
        Log.v(TAG, "myfacebook.reloadallfriends Not ready yet!");
        return new Report(false, "Not ready yet!");
    }

    Bundle params = new Bundle();
    params.putString("fields", "id,name,birthday,picture");
    mAsyncRunner.request("me/friends", params, new MyRequestListener(
            RequestType.FRIEND_LIST));

    Log.v(TAG, "myfacebook.reloadallfriends Fetch started.");
    return new Report(true, "Fetch started");
}

public List<MyFriend> getAllFriends() {
    return getFilteredFriends(null);
}

public List<MyFriend> getFilteredFriends(com.january.floogoo.Filter week) {
    return Main.db.getFriendsFilteredBy(week);
}

public List<Map<String, String>> getAllFriendsAsMap() {
    return getFilteredFriendsAsMap(null);
}

 public List<Map<String, String>> getFilteredFriendsAsMap(Filter filterBy) {
    List<MyFriend> friendList = Main.db.getFriendsFilteredBy(filterBy);

    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    for (MyFriend friend : friendList) {
        list.add(friend.getMap());
    }
    return list;
}

public void post(String receiver, String message) {
    if (isReady) {
        Bundle params = new Bundle();
        params.putString("message", message);

        mAsyncRunner.request(receiver + "/feed", params, "POST",
                new MyRequestListener(RequestType.FEED_POST));
    }
}

class MyAuthorizeListener extends BaseDialogListener {
    public void onComplete(Bundle values) {
        Log.i(TAG, "Authorization successfull");
        isReady = true;
        main.loadContents();
    }
}

class MyRequestListener extends BaseRequestListener {
    private RequestType type;

    public MyRequestListener(RequestType type) {
        this.type = type;
    }

    public void onComplete(final String response) {
        try {
            switch (type) {
            case FRIEND_LIST:
                 Log.d(TAG, "myfacebook.friendlist Response: "
                 + response.toString());
                myFriends.clear();
    JSONArray jarr = Util.parseJson(response).getJSONArray(
                        "data");
                for (int i = 0; i < jarr.length(); i++) {
                    JSONObject json = jarr.getJSONObject(i);
                    String fbID = json.getString("id");
                    String name = json.getString("name");
                    String bday = json.optString("birthday");
                    String pic = json.getString("picture");

                myFriends.add(new MyFriend(fbID, name, bday, pic));
                }
                main.notifyMain(Note.FRIENDLIST_RELOADED); 
                break;
            case FEED_POST:
                Log.d(TAG, "myfacebook.feedpost Response: "
                        + response.toString());
                break;
            default:
                break;
            }
        } catch (JSONException e) {
            Log.e(TAG, "JSONException: " + e.getMessage());
        } catch (FacebookError e) {
            Log.e(TAG, "FacebookError: " + e.getMessage());
        }
    }
}
    public class MyFriend {
private String fbID = " ";
private String name = " ";
private String bday = " "; // can be missing, be in form of mm/dd or
                           // mm/dd/yyyy
private String pic = " ";
private String bdayMessage = " ";
private boolean isAutoPost = false;

public MyFriend() {

}
MyFacebook.java

 TextView name = (TextView) v.findViewById(R.id.label);
    name.setText(friend.getName());

    TextView bday = (TextView) v.findViewById(R.id.label2);
    if (!friend.getBday().trim().equals("")) {
        bday.setText(friend.getBday());
    } else {
        bday.setText("Not Mentioned");
    }

    return v;
}
  public void init(Main main) {
    this.main = main;
    mFacebook = new Facebook(APP_ID);
    isReady = false;
    mAsyncRunner = new AsyncFacebookRunner(mFacebook);
    mFacebook.authorize(main, new String[] { "publish_stream",
            "friends_birthday" }, new MyAuthorizeListener());
}

// ref- http://developers.facebook.com/docs/reference/api/user/
public Report reLoadAllFriends() {
    if (!isReady) {
        Log.v(TAG, "myfacebook.reloadallfriends Not ready yet!");
        return new Report(false, "Not ready yet!");
    }

    Bundle params = new Bundle();
    params.putString("fields", "id,name,birthday,picture");
    mAsyncRunner.request("me/friends", params, new MyRequestListener(
            RequestType.FRIEND_LIST));

    Log.v(TAG, "myfacebook.reloadallfriends Fetch started.");
    return new Report(true, "Fetch started");
}

public List<MyFriend> getAllFriends() {
    return getFilteredFriends(null);
}

public List<MyFriend> getFilteredFriends(com.january.floogoo.Filter week) {
    return Main.db.getFriendsFilteredBy(week);
}

public List<Map<String, String>> getAllFriendsAsMap() {
    return getFilteredFriendsAsMap(null);
}

 public List<Map<String, String>> getFilteredFriendsAsMap(Filter filterBy) {
    List<MyFriend> friendList = Main.db.getFriendsFilteredBy(filterBy);

    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    for (MyFriend friend : friendList) {
        list.add(friend.getMap());
    }
    return list;
}

public void post(String receiver, String message) {
    if (isReady) {
        Bundle params = new Bundle();
        params.putString("message", message);

        mAsyncRunner.request(receiver + "/feed", params, "POST",
                new MyRequestListener(RequestType.FEED_POST));
    }
}

class MyAuthorizeListener extends BaseDialogListener {
    public void onComplete(Bundle values) {
        Log.i(TAG, "Authorization successfull");
        isReady = true;
        main.loadContents();
    }
}

class MyRequestListener extends BaseRequestListener {
    private RequestType type;

    public MyRequestListener(RequestType type) {
        this.type = type;
    }

    public void onComplete(final String response) {
        try {
            switch (type) {
            case FRIEND_LIST:
                 Log.d(TAG, "myfacebook.friendlist Response: "
                 + response.toString());
                myFriends.clear();
    JSONArray jarr = Util.parseJson(response).getJSONArray(
                        "data");
                for (int i = 0; i < jarr.length(); i++) {
                    JSONObject json = jarr.getJSONObject(i);
                    String fbID = json.getString("id");
                    String name = json.getString("name");
                    String bday = json.optString("birthday");
                    String pic = json.getString("picture");

                myFriends.add(new MyFriend(fbID, name, bday, pic));
                }
                main.notifyMain(Note.FRIENDLIST_RELOADED); 
                break;
            case FEED_POST:
                Log.d(TAG, "myfacebook.feedpost Response: "
                        + response.toString());
                break;
            default:
                break;
            }
        } catch (JSONException e) {
            Log.e(TAG, "JSONException: " + e.getMessage());
        } catch (FacebookError e) {
            Log.e(TAG, "FacebookError: " + e.getMessage());
        }
    }
}
    public class MyFriend {
private String fbID = " ";
private String name = " ";
private String bday = " "; // can be missing, be in form of mm/dd or
                           // mm/dd/yyyy
private String pic = " ";
private String bdayMessage = " ";
private boolean isAutoPost = false;

public MyFriend() {

}

尝试使用
SimpleDataFormat
格式化日期:

if (!friend.getBday().trim().equals("")) {
    SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");   
    Date dateObj = curFormater.parse(bday.setText(friend.getBday()));   
    SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy");   
    String newDateStr = postFormater.format(dateObj);
    bday.setText(newDateStr); // <--------- Set your date after formatting.
} else {
    bday.setText("Not Mentioned");
}
if(!friend.getBday().trim().equals(“”){
SimpleDataFormat curFormatter=新的SimpleDataFormat(“dd/MM/yyyy”);
Date dateObj=curformatter.parse(bday.setText(friend.getBday());
SimpleDataFormat后格式化程序=新的SimpleDataFormat(“MMMM dd,yyyy”);
字符串newDateStr=postformator.format(dateObj);
bday.setText(newDateStr);//Ref:

你需要这样的东西:

try {
  DateFormat df1 = new SimpleDateFormat("MM/dd/yyyy");
  DateFormat df2 = new SimpleDateFormat("MMM dd, yyyy");
  return df2.format(df1.parse(input));
}
catch (ParseException e) {
  return null;
}
======================================== 试试这个:

    TextView bday = (TextView) v.findViewById(R.id.label2);
    if (!friend.getBday().trim().equals("")) {

        try {
            // If this line is in loop you can declare thie two SimpleDateFormat //before loop
            SimpleDateFormat df1 = new SimpleDateFormat("MM/dd/yyyy");
            SimpleDateFormat df2 = new SimpleDateFormat("MMMM dd, yyyy");

            bday.setText(df2.format(df1.parse(friend.getBday().trim())));

            Log.i("Nimish", df2.format(df1.parse("12/13/1990")));

        } catch (ParseException e) {
            bday.setText("Not Mentioned");
            Log.i("Nimish", "" + e);
        }

    } else {
        bday.setText("Not Mentioned");
    }
输出:
01-04 17:51:29.299:I/Nimish(5042):1990年12月13日

将日期解析为日期对象,然后将日期格式化为您希望它显示的字符串。@meh我已经尝试过好几次了,给我一些代码看看我需要在代码中写些什么,然后我会在程序中做一些更改,在我没有得到我想要的东西的情况下,我正在写这个--TextView bday=(TextView)v.findViewById(R.id.label2);如果(!friend.getBday().trim().equals(“”){bday.setText(friend.getBday());}否则{bday.setText(“未提及”);}并以:02/25/1991的形式获得输出,因此更改我的代码我必须编写什么来实现这个目标,比如:1991年2月25日我已经尝试过buddy,就像我写的这个例子一样--TextView bday=(TextView)v.findViewById(R.id.label2);if(!friend.getBday().trim().equals(“”){bday.setText(friend.getBday());}else{bday.setText(“未提及”);}并以:02/25/1991的形式获得输出,因此更改我必须编写的代码以实现该目标,如:1991年2月25日