By reading this article you can learn how the micro-blogging sites (facebook, twitter..etc) displaying the date of an Article or Post in user's perspective. If any one posted a story in facebook, any user from any time zone should see the same time difference-message like "5 minutes ago" or  "1 hour ago" or  "yesterday" or "last month"..etc. 

Client's (User's) Time Zone

Get Client's "Locale" object by HttpServletRequest. Generate Calendar object by using that Locale object.
Locale clientLocale = (HttpServletRequest)request.getLocale();
Calendar client = Calendar.getInstance(clientLocale);

Article Posted Time 

Now get Calender object for posted time of an article. Here "millis" is article posted time in milli seconds.
Calendar currentTime = Calendar.getInstance();
client.setTimeInMillis(millis);

Calculation

Calculate the difference of time in milliseconds
long milliseconds1 = millis;
long milliseconds2 = client.getTimeInMillis();
long diff = milliseconds2 - milliseconds1;
long diffMinutes = diff / (60 * 1000);
long diffHours = diff / (60 * 60 * 1000);

Display the difference of time in minutes
if(diffMinutes>0 && diffMinutes < 60)
{
   if(diffMinutes==1)
       return "1  minute ago";
   else
       return diffMinutes+"  minutes ago";
}

Display the difference of time in hours
if(diffHours >0 && diffHours < 24)
{
    if(diffHours==1)
 return "1 hour ago";
    else
        return diffHours+" hours ago";
}

Display Year, Month, Days difference :
int temp;
int postedYear = client.get(Calendar.YEAR);
int currentYear = currentTime.get(Calendar.YEAR);
temp=currentYear-postedYear;
if(temp>0)
{
 if(temp==1)
   return "last year";
 else
   return temp+" years ago";
}

int postedMonth = client.get(Calendar.MONTH);
int currentMonth = currentTime.get(Calendar.MONTH);
temp=currentMonth-postedMonth;
if(temp>0)
{
 if(temp==1)
   return "last month";
 else
   return temp+" months ago";
}
int postedDate = client.get(Calendar.DATE);
int currentDate = currentTime.get(Calendar.DATE);
temp=currentDate-postedDate;
if(temp>0)
{
 if(temp==1)
   return "yester day";
 else
   return temp+" days ago";
}
return "just now";

Whole Code of the Function

// Pass the parameters "request (HttpServletRequest)" and "long (millis)" (Posted Time of the article in milliseconds)            
      public static String getPostedTimeInUsersPersperctive(
            HttpServletRequest request,long millis) {
                
  // Get Locale of the client 
  Locale clientLocale = request.getLocale();
                
  // Get caledar instance from locale
  Calendar client = Calendar.getInstance(clientLocale);
  Calendar currentTime = Calendar.getInstance();
  client.setTimeInMillis(millis);
  long milliseconds1 = millis;
  long milliseconds2 = c.getTimeInMillis();
  
  // Difference in time
  long diff = milliseconds2 - milliseconds1;
  long diffMinutes = diff / (60 * 1000);
  long diffHours = diff / (60 * 60 * 1000);
  
                // Difference in minutes
  if(diffMinutes>0 && diffMinutes < 60)
  {
   if(diffMinutes==1)
     return "1  minute ago";
   else
     return diffMinutes+"  minutes ago";
  }
  
                
                // Difference in hours
  if(diffHours >0 && diffHours < 24)
  {
   if(diffHours==1)
     return "1 hour ago";
   else
     return diffHours+" hours ago";
  }
  
         
  int temp;
  int postedYear = client.get(Calendar.YEAR);
  int currentYear = currentTime.get(Calendar.YEAR);
  temp=currentYear-postedYear;
  if(temp>0)
  {
   if(temp==1)
     return "last year";
   else
     return temp+" years ago";
  }

  int postedMonth = client.get(Calendar.MONTH);
  int currentMonth = currentTime.get(Calendar.MONTH);
  temp=currentMonth-postedMonth;
  if(temp>0)
   {
     if(temp==1)
       return "last month";
     else
       return temp+" months ago";
   }
  int postedDate = client.get(Calendar.DATE);
  int currentDate = currentTime.get(Calendar.DATE);
  temp=currentDate-postedDate;
  if(temp>0)
  {
   if(temp==1)
     return "yester day";
   else
     return temp+" days ago";
  }
  return "just now";
  }

1 comment:

Blogroll

Popular Posts