Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Calendar;

import java.util.Date;

public class Main {
    /**
     * Get passed time from given date
     *
     * @param date
     * @return
     */
    public static String getTimeString(Date date) {
        String strTime = "";
        Calendar thatDay = Calendar.getInstance();
        thatDay.setTime(date);

        Calendar today = Calendar.getInstance();

        long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis

        long second = diff / 1000;
        int min = (int) second / 60;
        int hour = min / 60;
        int day = hour / 24;
        int month = day / 30;
        int year = month / 12;

        if (min < 60) {
            strTime = String.format("%d min", min);
        } else if (min >= 60 && min < 60 * 24) {
            if (hour < 24) {
                strTime = String.format("%d hour", hour);
            }
        } else if (day < 31) {
            strTime = String.format("%d day", day);
        } else if (month < 12) {
            strTime = String.format("%d month", month);
        } else {
            strTime = String.format("%d year", year);
        }

        return strTime;
    }
}