i m making facebook app n i m getting update_date=2011-03-23T04:30:43+0000 in this formate now i want time ago same as facebook in browser from this date .. i m using date formate its working fine in morning but after 5.30 it is not working fine. i think its problem in GMT but i cant find any solution so plz help me...
thanks in advance.. CapDroid.
I am using this code... where str = 2011-03-23T04:30:43+0000
static public String dateformate(String str)
{
SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss+SSSS");
java.util.Date dat开发者_如何学编程e = null;
try
{
date = form.parse(str);
}
catch (ParseException e)
{
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("India"),Locale.getDefault());
calendar.set(Calendar.DATE,date.getDate());
calendar.set(Calendar.MONTH,date.getMonth());
calendar.set(Calendar.YEAR, date.getYear()+1900);
calendar.set(Calendar.HOUR,date.getHours());
calendar.set(Calendar.MINUTE,date.getMinutes());
calendar.set(Calendar.SECOND,date.getSeconds());
final long current = System.currentTimeMillis();
final long update = calendar.getTimeInMillis();
final long timeago = Math.abs(current-update);
if(timeago<=60000)
{
int time = (int)(timeago/1000);
return Integer.toString(time)+" seconds ago";
}
else if(timeago<=3600000 && timeago>=60000)
{
int time = (int)(timeago/60000);
return Integer.toString(time)+" minutes ago";
}
else if(timeago<=86400000 && timeago>=3600000)
{
int time = (int)(timeago/3600000);
return Integer.toString(time)+" hour ago";
}
}
If I get this right you want to tell your users how long ago some posting was made?
Why not convert the timestamp you get from Facebook into a unixtime timestamp and work with the difference to the current timestamp?
private static long convertDateToUnixTimeStamp(Date date) {
return date.getTime() / 1000L;
}
try this code snippet
Calendar calendar = Calendar.getInstance();
TimeZone timezone = TimeZone.getDefault();
TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
int currentGMTOffset = timezone.getOffset(calendar.getTimeInMillis());
int gmtOffset = utcTimeZone.getOffset(calendar.getTimeInMillis());
calendar.setTimeInMillis(calendar.getTimeInMillis() + (gmtOffset - currentGMTOffset));
final long current = calendar.getTimeInMillis();
// final long update = calendar.getTimeInMillis();
final long update = date.getTime();
final long timeago = Math.abs(current-update);
精彩评论