我怎么能转换成Facebook发布created_time用户的时区? [英] How can I convert the facebook post created_time to the time zone of the user?

查看:197
本文介绍了我怎么能转换成Facebook发布created_time用户的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我显示我在使用Facebook SDK的Andr​​oid应用程序Facebook的职位。我有created_time,格式为每个岗位:

I am displaying Facebook posts in my Android application using the Facebook SDK. I have the created_time for each post in the format:

2012-01-04T12:28:52+0000

我能够解析到这一个Java Date对象,像这样:

I was able to parse this into a Java Date object like so:

public static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss+SSSS";
public static Date parseDate(String str) {
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);

    try {
        Date date = sdf.parse(str);
    } catch(ParseException e) {
        Log.w(TAG, "Unable to parse date string \"" + str + "\"");
    }
    return date;
  }

于是,我试图显示描述后所经过的职位是使用此方法创建时的消息:

Then, I am attempting to display a message describing the time that has elapsed since the post was created using this method:

public static String timeAgoInWords(Date from) {
    Date now = new Date();
    long difference = now.getTime() - from.getTime();
    long distanceInMin = difference / 60000;

    if ( 0 <= distanceInMin && distanceInMin <= 1 ) {
        return "Less than 1 minute ago";
    } else if ( 1 <= distanceInMin && distanceInMin <= 45 ) {
        return distanceInMin + " minutes ago";
    } else if ( 45 <= distanceInMin && distanceInMin <= 89 ) {
        return "About 1 hour";
    } else if ( 90 <= distanceInMin && distanceInMin <= 1439 ) {
        return "About " + (distanceInMin / 60) + " hours ago";
    } else if ( 1440 <= distanceInMin && distanceInMin <= 2529 ) {
        return "1 day";
    } else if ( 2530 <= distanceInMin && distanceInMin <= 43199 ) {
        return (distanceInMin / 1440) + "days ago";
    } else if ( 43200 <= distanceInMin && distanceInMin <= 86399 ) {
        return "About 1 month ago";
    } else if ( 86400 <= distanceInMin && distanceInMin <= 525599 ) {
        return "About " + (distanceInMin / 43200) + " months ago";
    } else {
        long distanceInYears = distanceInMin / 525600;
        return "About " + distanceInYears + " years ago";
    }
}

问题是,帖子的CREATE_TIME是超前的我的时间(EST)6小时。因此,timeAgoInWords方法不能正常工作。难道Facebook的随时记录在UTC / GMT1小时职位的创建时间?

The problem is that the create_time of the post is 6 hours ahead of my time (EST). So the timeAgoInWords method doesn't work correctly. Does Facebook always record the creation time of posts in UTC/GMT +1 hour?

我能做些什么来转换的创建时间,这样timeAgoInWords将正常工作为当前用户的时区,无论他们在哪里?

What can I do to convert the creation time so that timeAgoInWords will work correctly for the current user's timezone, no matter where they are?

推荐答案

这是在code我使用UTC时间字符串转换为一个本地化的时间字符串。需要注意的关键转换是使用时区方法 getRawOffset() inDaylightTime(。 ..) getDSTSavings()

This is the code I use to convert a UTC time string to a localized time string. Note the key for conversion is to use the TimeZone methods getRawOffset(), inDaylightTime(...) and getDSTSavings().

请注意我的日期格式稍有不同,以您使用的是什么,所以你需要改变它。

Note my date format is slightly different to what you are using so you'll need to change it.

public String GetLocalDateStringFromUTCString(String utcLongDateTime) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String localDateString = null;

    long when = 0;
    try {
        when = dateFormat.parse(utcLongDateTime).getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    localDateString = dateFormat.format(new Date(when + TimeZone.getDefault().getRawOffset() + (TimeZone.getDefault().inDaylightTime(new Date()) ? TimeZone.getDefault().getDSTSavings() : 0)));

    return localDateString;
}

显然,你希望有一个日期对象,所以你只需要改变这一行...

Obviously you want a Date object so you just need to change this line...

localDateString = dateFormat.format(new Date(when + TimeZone.getDefault().getRawOffset() + (TimeZone.getDefault().inDaylightTime(new Date()) ? TimeZone.getDefault().getDSTSavings() : 0)));

...到...

...to...

Date localDate = new Date(when + TimeZone.getDefault().getRawOffset() + (TimeZone.getDefault().inDaylightTime(new Date()) ? TimeZone.getDefault().getDSTSavings() : 0));

...那么你只需要改变方法的返回类型并返回 localDate

这篇关于我怎么能转换成Facebook发布created_time用户的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆