title: How to Show time difference or reverse time in android
body: <pre><code>class revrsetime {
  
      public static void main(String[] args) {
  
          System.out.println(setLastSeenTime("26/08/2014 14:29:00"));
  
      }
  
      public static String setLastSeenTime(String time) {
  
          long milliseconds = Math.abs(System.currentTimeMillis()
                  - converTimeStringINToMillis(time));
  
          String lastSeen = "";
  
          long seconds = (long) milliseconds / 1000;
          if (seconds &lt; 60)
              lastSeen = String.valueOf(seconds) + "sec ago";
          else if (seconds &gt; 60 &amp;&amp; seconds &lt; 3600)
              lastSeen = String.valueOf((int) seconds / 60) + " min ago";
          else if (seconds &gt; 3600 &amp;&amp; seconds &lt; 86400)
              lastSeen = String.valueOf((int) seconds / 3600) + " hours ago";
          else if (seconds &gt; 86400 &amp;&amp; seconds &lt; 172800)
              lastSeen = " Yesterday";
          else if (seconds &gt; 172800 &amp;&amp; seconds &lt; 2592000)
              lastSeen = String.valueOf((int) (seconds / (24 * 3600)))
                      + " days ago";
          else if (seconds &gt; 2592000)
              lastSeen = String.valueOf((int) (seconds / (30 * 24 * 3600)))
                      + " months ago";
  
          return lastSeen;
  
      }
  
      private static long converTimeStringINToMillis(String time) {
  
          long milliseconds = 0;
  
          try {
              SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
              sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
  
              // 25/06/2014 8:41:26
  
              Date date;
  
              date = sdf.parse(time);
              milliseconds = date.getTime();
          } catch (ParseException e) {
              // TODO Auto-generated catch block
              milliseconds = 0;
              e.printStackTrace();
          }
  
          return milliseconds;
      }
  
  }
  </code></pre>
  
  <p>This my code for display Lastseendate and time what I am doing this I have getting time from web service and system time make it differences. In some cases I am getting the correct Output but in others I am not.</p>
  
  <p>The output when I set date and time : 26/08/2014 13:29:00 then it shows 3 hours ago when it should show 2 hours ago. When I set 26/08/2014 14:29:00 then it shows 4 hours ago.</p>
  
  <p>I don't know where I'm making the mistake, please help.</p>