- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 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/
在我的 Rails (3.0) 应用程序中,我使用助手 time_zone_select() 来使用时区。它生成时区名称,如“(GMT+01:00) Paris”... 但这些名称与 MySQL 中的
我正在解析格式为的日期: SimpleDateFormat formatter = new SimpleDateFormat("EE MMM dd HH:mm:ss Z yyyy"); 当我这样做的时
我在 Android 上使用 ACTION_TIMEZONE_CHANGED Intent 过滤器来响应时区更改。 我注意到 Jodas 当前时区此时没有更新,使用:DateTimeZone.getD
我有一个旧版 Windows 应用程序,它从数据库读取数据。其中一列是“TimeZoneInfoId”。在传统世界中,它是由另一个 Windows 应用程序编写的,因此它存储 Windows 字符串:
我是数据库开发新手,目前从事 MySQL 工作。我有一列包含美国的所有时区。我希望能够获取每行中每个时区的当前时间(只有时间,没有日期)。 时区数据如下:+05:00、-03:00 等等 这就是我尝试
我正在尝试了解 session 时区在 PostgreSQL 中的工作原理。 当我将 session 时区设置为“01:00”时,返回日期为“-01:00”。符号总是颠倒的,我不理解这种行为。 这怎么
我写入我的 mongoDB 数据库,例如开始日期,开始日期始终是一周中的星期一(我使用并且我在欧洲中部时区)。 "startDate" : ISODate("2014-12-28T23:00:00Z"
是否有详尽的 MySQL 时区列表? 似乎 MySQL 设置中 time_zone 的有效值取决于主机操作系统,但我一直无法找到可能值的列表。 我需要时间显示卡尔加里本地时间。 最佳答案 默认情况下,
我已经在 config/app.php 中配置了时区至 Europe/Lisbon . 如果我做 return date_default_timezone_get(); , 返回 Europe/Lis
我在整个脚本中广泛使用 unix 时间,通常我将时区设置如下: date_default_timezone_set('Europe/London'); 这一直工作正常,直到上周末时钟回退一个小时,我怀
我有一个字符串,2013-10-07T23:59:51.205-07:00,想将其转换为 Java 日期对象。我收到解析错误。 date = new SimpleDateFormat("yyyy-MM
如何更改日志文件名中的 log4j 时区? 我的 log4j.xml 文件:
在JAVA中,如何确定所有日期都作为GMT日期返回? 例如,即使我尝试使用GMT语言环境强制使用DateFormat,它也会应用某种逻辑来检索BST日期。 public static void mai
好吧,这是我遇到过的最奇怪的错误之一! 首先:我不是 Python 程序员,该脚本是由 friend 编写的(我认为他的大部分内容来自示例)。 该脚本的用途:将日历信息(CET 时间)从 XML 文件
我们有一个网站,目前在某个时间(由用户选择)运行拍卖,这一切都工作正常,因为当服务器到达该时间时,拍卖就会开始。然后我们必须添加时区,具体取决于用户居住的地方。然后,根据所选的下拉菜单,这将增加或减少
我陷入了这个逻辑。我正在开发一个消息调度项目,我的客户来自不同的国家。让我们考虑一下亚洲(不遵守夏令时)和美国/纽约(遵守夏令时)。 现在,我正在编写以下查询来获取距当前时间 10 分钟间隔内的时间表
我尝试返回特定时区的值,但我遇到了一些奇怪的响应行为: SELECT created_at AT TIME ZONE 'US/Pacific' - created_at, NOW() A
我正在尝试将不带时区值(例如“31.05.2015”)的字符串转换为 NSDate。 我的 DateFormatter 有什么问题?它返回日期 2015-05-31 22:00:00 +0000当我尝
我已经成功地将我的本地 tzinfo 导入到 mysql 中,我几乎已经了解了关于如何存储和使用这些数据的所有细节,除了一个字段。有一个名为 Transition_type_id 的数字字段(在表 t
为什么以下行返回的 TimeZone 显示不正确的时间: TimeZone.getTimeZone("America/Ottawa") 现在显示晚上 10:26 [亚特兰大时间现在是 6:26,Ott
我是一名优秀的程序员,十分优秀!