Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.concurrent.TimeUnit;

public class Main {
    public static String translatePastTimeIntoHumanReadableFormat(long pastTime) {
        long time = System.currentTimeMillis() - pastTime;
        if (pastTime == 0 || time < 0) {
            return "Just Now";
        } else {
            long seconds = TimeUnit.MILLISECONDS.toSeconds(time);
            long minutes = TimeUnit.MILLISECONDS.toMinutes(time);
            long hours = TimeUnit.MILLISECONDS.toHours(time);
            long days = TimeUnit.MILLISECONDS.toDays(time);
            long years = (int) days / 365;

            if (years > 0) {
                if (years == 1) {
                    return "1 year ago";
                } else {
                    return String.format("%d years ago", years);
                }
            } else if (days > 0) {
                if (days == 1) {
                    return "Yesterday";
                } else {
                    return String.format("%d days ago", days);
                }
            } else if (hours > 0) {
                if (hours == 1) {
                    return "About an hour ago";
                } else {
                    return String.format("%d hours ago", hours);
                }
            } else if (minutes > 0) {
                if (minutes == 1) {
                    return "About one minute ago";
                } else {
                    return String.format("%d minutes ago", minutes);
                }
            } else {
                return "Just Now";
            }
        }
    }
}