I am parsing twitters and i want to display how long ago it was since the twitter was posted. But it doesn't seem to be calculating correctly. I got the formula from another post here on Stackoverflow and i tried to build the return statement from it.
public static String getTwitterDate(Date date){
long milliseconds = date.getTime();
int minutes = (int) ((milliseconds / (1000*60)) % 60);
int hours = (int) ((milliseconds / (1000*60*60)) % 24);
if (hours > 0){
if (hours == 1)
return "1 hour ago";
else if (hours < 24)
return String.valueOf(hours) + " hours ago";
else
{
int days = (int)Math.ceil(hours % 24);
if (days == 1)
return "1 day ago";
else
return String.valueOf(days) + " days ago";
}
}
else
{
if (minutes == 0)
return "less than 1 minute ago";
else if (minutes == 1)
return "1 minute ago";
else
return String.valueOf(minutes) + " minutes ago";
}
}
Parsing the twitter date/time with this (also from a post on Stackoverflow)
public static Date parseTwitterDate(String date)
{
final String TWITTER = "EEE, dd MMM yyyy HH:mm:ss Z";
SimpleDateFormat sf = new SimpleDateFormat(TWITTER, Locale.ENGLISH);
sf.setLenient(true);
try {
return sf.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
Example of a twitter date: "created_at":"Sat, 30 Jun 2012 14:44:40 +0000",
As far as i can tell, the twitters are getting parsed correctly but not calculated correctly (getTwitterDate). That returns 11 hours some times when its a difference of 4-5 hours.
long difference = longNow - date. getTime(); int msPerHour = 1000*60*60; int hours = difference/secondPerHour; int minutes = difference % secondPerHour; where longNow is your current variable containing system time.
I need to Calculate the TotalTime by subtracting the EndTime with StartTime. The Format of StartTime and EndTime is as like follows: StartTime = "08:00 AM"; EndTime = "04:00 PM"; TotalTime in Hours and Mins Format.
Calculate the duration between two times The goal is to subtract the starting time from the ending time under the correct conditions. If the times are not already in 24-hour time, convert them to 24-hour time. AM hours are the same in both 12-hour and 24-hour time.
How to Convert Hours to Minutes. To convert an hour measurement to a minute measurement, multiply the time by the conversion ratio. The time in minutes is equal to the hours multiplied by 60.
Use DateUtils. it is fully localized and does what you want to do in a human-friendly manner.
Here is an example for getting a relative time span expression that is automatically localized:
long time = ... // The Twitter post time stamp
long now = System.currentTimeMillis();
CharSequence relativeTimeStr = DateUtils.getRelativeTimeSpanString(time,
now, DateUtils.SECOND_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE);
It produces output such as "10 seconds ago" or "in 5 minutes".
long milliseconds = date.getTime() - new Date().getTime();
that will run the calculation based on the difference between the date of the tweet and now.
At the moment, you are basing your calculation on date.getTime()
which is the number of milliseconds since midnight on 1-Jan-1970. Because you also introduces modulos, your current function gives you the time elapsed between midnight UTC and the tweet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With