gpt4 book ai didi

java - 如何将 facebook post created_time 转换为用户的时区?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:32:10 25 4
gpt4 key购买 nike

我正在使用 Facebook SDK 在我的 Android 应用程序中显示 Facebook 帖子。我有每个帖子的 created_time,格式如下:

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

我能够像这样将其解析为 Java Date 对象:

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;
}

然后,我尝试显示一条消息,描述自使用此方法创建帖子以来耗时:

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";
}
}

问题是帖子的创建时间比我的时间 (EST) 提前 6 小时。所以 timeAgoInWords 方法不能正常工作。 Facebook 是否始终以 UTC/GMT +1 小时记录帖子的创建时间?

我可以做些什么来转换创建时间,以便 timeAgoInWords 能够在当前用户的时区正常工作,无论他们身在何处?

最佳答案

这是我用来将 UTC 时间字符串转换为本地化时间字符串的代码。请注意,转换的关键是使用 TimeZone 方法 getRawOffset()inDaylightTime(...)getDSTSavings().

请注意我的日期格式与您使用的略有不同,因此您需要更改它。

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;
}

显然您需要一个Date 对象,因此您只需更改这一行...

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

...到...

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

...那么您只需更改方法的返回类型并返回 localDate

关于java - 如何将 facebook post created_time 转换为用户的时区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8734932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com