Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Date;
- /**
- * Created by misael on 7/25/2015.
- */
- public class Time {
- private long time_,seconds,minutes,hours,days,weeks,months,years;
- private String time;
- public String getTimeAgo(long cur_time) {
- long currentDateMS = new Date().getTime();
- long sec = (long) (currentDateMS / 1000);
- time_ = sec - cur_time;
- seconds = time_;
- minutes = time_ / 60;
- hours = time_ / 3600;
- days = time_ / 86400;
- weeks = time_ / 604800;
- months = time_ / 2419200;
- years = time_ / 29030400;
- if (seconds <= 60) {
- if (seconds >= 0) {
- time = "Just.";
- }
- else if (seconds <=30) {
- time = Long.toString(seconds) + " second ago.";
- }
- }
- else if (minutes <= 60) {
- if (minutes == 1) {
- time = "One minute ago.";
- }
- else{
- time = Long.toString(minutes)+ " minute ago.";
- }
- }
- else if (hours <= 24) {
- if (hours == 1) {
- time = "One hour ago";
- }
- else{
- time = Long.toString(hours) + " hour ago.";
- }
- }
- else if (days <= 7) {
- if (days == 1) {
- time = "Yesterday.";
- }
- else{
- time = Long.toString(days) + " day ago.";
- }
- }
- else if (weeks <= 4) {
- if (weeks == 1) {
- time = "One week ago.";
- }
- else{
- time = Long.toString(weeks) + " week ago.";
- }
- }
- else if (months <= 12) {
- if (months == 1) {
- time = "One month ago.";
- }
- else{
- time = Long.toString(months) + " month ago.";
- }
- }
- else {
- if ($years == 1) {
- time = "One year ago";
- }
- else{
- time = Long.toString(years) + " year ago";
- }
- }
- return time;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement