Advertisement
ferdhika31

Time Ago

Aug 8th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Date;
  2. /**
  3.  * Created by misael on 7/25/2015.
  4.  */
  5. public class Time {
  6.     private long time_,seconds,minutes,hours,days,weeks,months,years;
  7.     private String time;
  8.     public String getTimeAgo(long cur_time) {
  9.         long currentDateMS = new Date().getTime();
  10.         long sec = (long) (currentDateMS / 1000);
  11.         time_ = sec - cur_time;
  12.         seconds = time_;
  13.         minutes = time_ / 60;
  14.         hours = time_ / 3600;
  15.         days = time_ / 86400;
  16.         weeks = time_ / 604800;
  17.         months = time_ / 2419200;
  18.         years = time_ / 29030400;
  19.  
  20.         if (seconds <= 60) {
  21.             if (seconds >= 0) {
  22.                 time = "Just.";
  23.             }
  24.             else if (seconds <=30) {
  25.                 time = Long.toString(seconds) + " second ago.";
  26.             }
  27.         }
  28.         else if (minutes <= 60) {
  29.             if (minutes == 1) {
  30.                 time = "One minute ago.";
  31.             }
  32.             else{
  33.                 time = Long.toString(minutes)+ " minute ago.";
  34.             }
  35.         }
  36.         else if (hours <= 24) {
  37.             if (hours == 1) {
  38.                 time = "One hour ago";
  39.             }
  40.             else{
  41.                 time = Long.toString(hours) + " hour ago.";
  42.             }
  43.         }
  44.         else if (days <= 7) {
  45.             if (days == 1) {
  46.                 time = "Yesterday.";
  47.             }
  48.             else{
  49.                 time = Long.toString(days) + " day ago.";
  50.             }
  51.         }
  52.         else if (weeks <= 4) {
  53.             if (weeks == 1) {
  54.                 time = "One week ago.";
  55.             }
  56.             else{
  57.                 time = Long.toString(weeks) + " week ago.";
  58.             }
  59.         }
  60.         else if (months <= 12) {
  61.             if (months == 1) {
  62.                 time = "One month ago.";
  63.             }
  64.             else{
  65.                 time = Long.toString(months) + " month ago.";
  66.             }
  67.         }
  68.         else {
  69.             if ($years == 1) {
  70.                 time = "One year ago";
  71.             }
  72.              else{
  73.                 time = Long.toString(years) + " year ago";
  74.              }
  75.         }
  76.         return time;
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement