Convert time To Full String - Java java.util

Java examples for java.util:Time Format

Description

Convert time To Full String

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        long time = 42;
        System.out.println(timeToFullString(time));
    }/*w w  w  .j  a  v  a2  s  .c om*/

    public static final long ONE_WEEK_IN_MILISECONDS = 604800000L;
    public static final long ONE_DAY_IN_MILISECONDS = 86400000L;
    public static final long ONE_HOUR_IN_MILISECONDS = 3600000L;
    public static final long ONE_MINUTE_IN_MILISECONDS = 60000L;
    public static final long ONE_SECOND_IN_MILISECONDS = 1000L;

    /**
     *
     * @param time
     * @return
     */
    public static String timeToFullString(long time) {
        String returnTime = "";
        long temp;

        if (time / ONE_WEEK_IN_MILISECONDS > 0) {
            temp = time / ONE_WEEK_IN_MILISECONDS;
            String week = (temp > 1) ? temp + " weeks " : temp + " week ";
            returnTime += week;
            temp = (time - temp * ONE_WEEK_IN_MILISECONDS)
                    / ONE_DAY_IN_MILISECONDS;
            String day = (temp > 1) ? temp + " days " : temp + " day ";
            returnTime += day;

        } else if (time / ONE_DAY_IN_MILISECONDS > 0) {
            temp = time / ONE_DAY_IN_MILISECONDS;
            String day = (temp > 1) ? temp + " days " : temp + " day ";
            returnTime += day;
            temp = (time - temp * ONE_DAY_IN_MILISECONDS)
                    / ONE_HOUR_IN_MILISECONDS;
            String hour = (temp > 1) ? temp + " hours " : temp + " hour ";
            returnTime += hour;

        } else if (time / ONE_HOUR_IN_MILISECONDS > 0) {
            temp = time / ONE_HOUR_IN_MILISECONDS;
            String hour = (temp > 1) ? temp + " hours " : temp + " hour ";
            returnTime += hour;
            temp = (time - temp * ONE_HOUR_IN_MILISECONDS)
                    / ONE_MINUTE_IN_MILISECONDS;
            String minute = (temp > 1) ? temp + " minutes " : temp
                    + " minute ";
            returnTime += minute;

        } else if (time / ONE_MINUTE_IN_MILISECONDS > 0) {
            temp = time / ONE_MINUTE_IN_MILISECONDS;
            String minute = (temp > 1) ? temp + " minutes " : temp
                    + " minute ";
            returnTime += minute;

        } else if (time / ONE_SECOND_IN_MILISECONDS > 0) {
            temp = time / ONE_SECOND_IN_MILISECONDS;
            String second = (temp > 1) ? temp + " seconds " : temp
                    + " second ";
            returnTime += second;
        } else {
            returnTime = "Going Live";
        }
        return returnTime;
    }
}

Related Tutorials